C#分部方法

C#分部方法必须是私有的,不能返回值。分部方法主要用内部信息处理中。

下面的例子,有一个分部类People,其中一个定义一个分部方法SetDefaultValue,另外一个类中实现了其中的逻辑处理。

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
People People1 = new People();
People1.Display();
Console.Read();
}
}

public partial class People
{
public string Name { get; set; }
public int Age { get; set; }
partial void SetDefaultValue();

public void Display()
{
SetDefaultValue();
Console.WriteLine(string.Format("{0}年龄是{1}", Name, Age));
}
}

public partial class People
{
partial void SetDefaultValue()
{
Name = "People1";
Age = 20;
}
}
}

原文地址:https://www.cnblogs.com/suizhikuo/p/4022726.html