您可以使用标准语法(使用
this类似方法的方法)在类 内部 选择重载:
class Foo { private int id; private string name; public Foo() : this(0, "") { } public Foo(int id, string name) { this.id = id; this.name = name; } public Foo(int id) : this(id, "") { } public Foo(string name) : this(0, name) { }}然后:
Foo a = new Foo(), b = new Foo(456,"def"), c = new Foo(123), d = new Foo("abc");另请注意:
- 您可以使用以下方式链接到基本类型的构造函数
base(...)
- 您可以在每个构造函数中添加额外的代码
- 默认值(如果您未指定任何内容)是
base()
对于“为什么?”:
- 代码减少(总是一件好事)
- __调用非默认基本构造函数 所必需 ,例如:
SomebaseType(int id) : base(id) {...}
请注意,您也可以以类似的方式使用对象初始化程序(无需编写任何内容):
SomeType x = new SomeType(), y = new SomeType { Key = "abc" }, z = new SomeType { DoB = DateTime.Today };


