开发者

Multiple Constructors for a Class Problem

开发者 https://www.devze.com 2022-12-20 15:47 出处:网络
I am used to doing this in C++. Is this not allowed in C#? BasicCtor(int a) { return BasicCtor(a, \"defaultStringValue\");

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

精彩评论

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