Strange property initialization

7

When reading a tutorial on Entity Framework, I came across an example code where there was a line that, for me, is unknown:

Student stud = new Student() { StudentName = "New Student" };

I understand that a new object of type Student is being instantiated, but the instructions in braces are strange to me. What is it? By chance a second form that C # has to initialize an object property?

In this case I'm familiar with the trivial way, ie through the constructor.

Student stud = new Student(StudentName = "New Student");
    
asked by anonymous 02.05.2016 / 16:29

1 answer

6

Object initializer

This is a object initializer . It is a way to start the members of an object independent of having a constructor.

It works in a way analogous to the constructor, but it can initialize any public member (it can not access private members unless indirectly with a public method, just like the constructor).

It basically has the same characteristics as a constructor, but the initialization order is not guaranteed (it depends on how the programmer uses the object's creation) as opposed to the constructor that determines the exact order of initialization, and can also perform auxiliary operations during startup.

Understand what a builder is for .

See a practical example .

In fact the syntax could be even simpler:

var stud = new Student { StudentName = "New Student" };

This is the same thing to do:

var stud = new Student();
stud.StudentName = "New Student";

But with the advantage that the object will only be available to the application at the end of the execution of the two lines. Written separately, a programmer can put something in the middle of those lines in future versions and stop doing what he expected.

Note that StudentName is a public property of the class (or a public field, which is rare).

Nothing guarantees that all necessary members will be initialized using this form. The constructor is better when this is important.

As you can see in documentation , the same syntax is possible with the initialization of collections.

Constructor with named arguments

The second syntax is a constructor like any other, but you are using a named argument . Coincidentally the parameter has the same property name, but make no mistake, there is the name of the parameter, not the property. The most appropriate one according to the style normally adopted in C # (and fixing the syntax error observed by Thiago Lunardi in comment - see below that the syntax may be correct) would be:

var stud = new Student(studentName : "New Student");

This syntax can help give better semantics to the construction of the object. Apparently in this case there is no greater advantage. This use is more advantageous when the method has several parameters and better still when some have default values. So you can use the arguments in the order you want and it is easier to omit the ones that already have values.

This syntax is entirely dependent on the builder signature .

Difference between parameter and argument .

Note that the syntax may be right. It's bad, it's confusing, but it could be doing a assignment from "New Student" to a variable named StudentName e using the value of this variable as the constructor argument. See running on dotNetFiddle .

Much of this can be avoided with fields, or even properties already initialized in class .

    
02.05.2016 / 16:42