开发者

Multiple Constructors for a Class Problem

I am used to doing this in C++. Is this not allowed in C#?

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

Bas开发者_高级运维icCtor(int a, string b)
{
    //blah blah

}

In C# I can neither return a calling of the constructor or call it w/o a return. Does C# allow what I want to do? :P


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

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

}


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

--

Note: This assumes C# 3.5 (In c#4. you may be able to omit the other constructor and use default parameters

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜