Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor vs Object Initializer Precedence in C#

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; } 
like image 955
bbill Avatar asked Jun 26 '13 18:06

bbill


People also ask

Is a constructor and initializer the same?

Object Initializers were something added to C# 3, in order to simplify construction of objects when you're using an object. Constructors run, given 0 or more parameters, and are used to create and initialize an object before the calling method gets the handle to the created object.

Which constructor does not initialize any data member?

The default constructor does not initialize members of your class.

Is assigned in constructor body consider performing initialization in initialization list?

Note: It is mandatory that a reference or a const member must be intialized in a constructor initialization list. They cannot be 'assigned' in the body of the constructor.

What is initialization in constructor?

Explicit initialization with constructors (C++ only) A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize non-static constant and reference class members.

What is the difference between constructor and initializer in C++?

A constructor is a defined method on a type which takes a specified number of parameters and is used to create and initialize an object. An object initializer is code that runs on an object after a constructor and can be used to succinctly set any number of fields on the object to specified values.

What is an object initializer in C5?

From the C# 5 language specification (7.6.10.1) Processing of an object creation expression that includes an object initializer or collection initializer consists of first processing the instance constructor and then processing the member or element initializations specified by the object initializer or collection initializer.

How do you initialize member variables in a constructor?

Although member initialization can occur in the constructor's body, it is considered best practice to initialize members with an initializer list. An initializer list has the advantage of running before the function's body. That means that the member variables are ready to use as soon as the constructor body runs.

How does the compiler process object initializers?

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).


2 Answers

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.

like image 108
Paul Sasik Avatar answered Sep 25 '22 17:09

Paul Sasik


Object initializers are just syntactic sugar, in your compiled assembly IL they translate into separate statements, check it on ILSpy.

enter image description here

like image 31
Tamim Al Manaseer Avatar answered Sep 22 '22 17:09

Tamim Al Manaseer