一类问题的多个构造函数

时间:2010-02-20 21:42:09

标签: c# constructor

我习惯在C ++中这样做。 C#中不允许这样做吗?

BasicCtor(int a)
{
   return BasicCtor(a, "defaultStringValue"); 
}

BasicCtor(int a, string b)
{
    //blah blah

}

在C#中,我既不能返回构造函数的调用,也不会调用它来返回。 C#允许我想做什么吗? :P

2 个答案:

答案 0 :(得分:6)

BasicCtor(int a) : this(a, "defaultStringValue")
{
}

BasicCtor(int a, string b)
{
    //blah blah

}

答案 1 :(得分:0)

Have you tried the following:

class MyClass {
  MyClass(int a) : this(a, "defaultStringValue")
  { 
     // Any additional constructur code (optional)
  } 

  MyClass(int a, string b) 
  { 
    //Original constructor
  } 
}

-

注意:这假定为C#3.5(在c#4中。您可以省略其他构造函数并使用默认参数

相关问题