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;
}
}
}

相关文章:

  • 2021-08-15
  • 2021-07-13
  • 2022-01-23
  • 2021-06-05
  • 2021-12-13
  • 2021-12-13
  • 2021-11-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-14
  • 2021-12-13
  • 2022-12-23
  • 2022-03-10
相关资源
相似解决方案