I've been learning the object initializer in C# recently, but now I'm wondering how it works when it conflicts with the constructor.
public class A
{
public bool foo { get; set; }
public A()
{
foo = true;
}
public A(bool bar)
{
foo = bar;
}
}
What happens when I try this?
public class B
{
private A a = new A() { foo = false };
private A b = new A(true) { foo = false };
}
Is a default in the constructor a good way to have a bool
that starts true and can be changed?
public A(bool bar = true)
{
foo = bar;
}
From the documentation:
The compiler processes object initializers by first accessing the default instance constructor and then processing the member initializations.
This means that in the simplest case (named object initialization) it is basically shorthand (or syntactic sugar) for calling the default constructor and then calling the property setter(s). In the case of anonymous types this kind of initialization is actually required and not mere sugar.
For the 2nd part of your question: It's more of a matter of style but if you have a crucial property I would not create a constructor with a default value. Make the client code set the value explicitly. I'm also not sure why doing something like this: b = A(true) {foo = false};
would be a good idea unless you're in a code obfuscation contest.
Bit of caution though:
... if the default constructor is declared as private in the class, object initializers that require public access will fail.