bobber205
bobber205

Reputation: 13382

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"); 
}

BasicCtor(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

Upvotes: 2

Views: 3838

Answers (2)

Steven_W
Steven_W

Reputation: 848

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

Upvotes: 0

John Saunders
John Saunders

Reputation: 161831

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

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

}

Upvotes: 6

Related Questions