Blankman
Blankman

Reputation: 266900

How to call other constructors from a constructor in c#?

I have a constructor like:

public Blah(string a, string b)
{

}

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

How can I call the 2nd constructor from the 1st one?

like:

public Blah(string a, string b)
{
   Blah(a,b, "");
}

Upvotes: 4

Views: 1180

Answers (4)

It Man
It Man

Reputation: 21

public Blah(string a, string b): this(a, b, String.Empty) {

}

Upvotes: -2

Kimi
Kimi

Reputation: 14099

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

Upvotes: 5

Charles Bretana
Charles Bretana

Reputation: 146409

public Blah(string a, string b) : this(a,b, "default_C_String")
{ 

} 

--- whatever your desired default value is for C ...

Upvotes: 1

LukeH
LukeH

Reputation: 269278

public Blah(string a, string b) : this(a, b, "")
{
}

public Blah(string a, string b, string c)
{
    // etc
}

Upvotes: 9

Related Questions