Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从c#中的构造函数调用其他构造函数?_C# - Fatal编程技术网

如何从c#中的构造函数调用其他构造函数?

如何从c#中的构造函数调用其他构造函数?,c#,C#,我有一个构造函数,比如: public Blah(string a, string b) { } public Blah(string a, string b, string c) { this.a =a; this.b =b; this.c =c; } 如何从第一个构造函数调用第二个构造函数 比如: ---无论您希望C的默认值是什么…公共Blah(字符串a,字符串b):这(a,b,字符串.Empty) { }顺便说一句,这通常被称为构造函数链接。最好使用String.Empt

我有一个构造函数,比如:

public Blah(string a, string b)
{

}

public Blah(string a, string b, string c)
{
  this.a =a;
  this.b =b;
  this.c =c;
}
如何从第一个构造函数调用第二个构造函数

比如:

---无论您希望C的默认值是什么…

公共Blah(字符串a,字符串b):这(a,b,字符串.Empty) {


}

顺便说一句,这通常被称为构造函数链接。最好使用
String.Empty
而不是“@abatishchev:你有参考资料吗?就我而言,这只是个人喜好和你觉得更可读的东西。我更喜欢
”(可能)创建一个对象,String.empty不会。@Anders:
对象。ReferenceEquals(“,String.empty)
在我的机器上计算为true-即,它们是同一个插入的字符串对象。(即使情况并非总是如此,我也可以在每个组件上多用一根微小的内接线。)是的,在大多数情况下,它的实际影响很小
public Blah(string a, string b)
{
   Blah(a,b, "");
}
public Blah(string a, string b) : this(a, b, "")
{
}

public Blah(string a, string b, string c)
{
    // etc
}
public Blah(string a, string b): this(a, b, String.Empty)
{

}

public Blah(string a, string b, string c)
{
  this.a =a;
  this.b =b;
  this.c =c;
}
public Blah(string a, string b) : this(a,b, "default_C_String")
{ 

}