Reputation: 506
What is the difference between these two fractions in C# 5.0 (both to the programmer and to the compiler and CLR):
public MyClass()
{
; // empty
}
public MyClass(int number = 1, string text = "hello")
{
; // empty
}
Upvotes: 0
Views: 89
Reputation: 11858
If you call the second ctor, then the generated MSIL contains the default parameters, like you had directly called the ctor with two parameters.
Default parameter values are only syntactical sugar of C#... and should not be used. Use overloaded methods/ctors instead.
Have a look at http://lostechies.com/jimmybogard/2010/05/18/caveats-of-c-4-0-optional-parameters/
Upvotes: 1
Reputation: 1208
It is just like any function, the constructor with parameters will need them. The compiler will automatically add a default constructor if none is provided, but only if no other constructor definition is supplied. If you include a constructor with parameters, you can no longer use the default constructor and must explicitly define it.
Upvotes: 0