Object initializer
That’s a Object initializer. It is a way to start members of an object independent of having a constructor.
It works analogously to the constructor, but it can initialize any public member (it cannot access private members, except indirectly with a public method, exactly as with the constructor).
It has basically the same characteristics as a constructor, but the initialization order is not guaranteed (it depends on how the programmer uses in the creation of the object) unlike the constructor that determines the exact initialization order, and can still perform ancillary operations during startup.
Understand what a builder is for.
See a practical example.
Actually the syntax could be even simpler:
var stud = new Student { StudentName = "New Student" };
That’s the same as doing:
var stud = new Student();
stud.StudentName = "New Student";
But with the advantage that the object will only be available for the application at the end of the execution of the two lines. Written separately a programmer can put something in the middle of these lines in future versions and stop doing what was expected.
Note that StudentName
is a public property of the class (or a public field, which is rare).
Nothing guarantees that all required members will be initialized properly using this form. The constructor is best when this is important.
As you can see in documentation the same syntax is possible with initialization of collections.
Constructor with named arguments
The second syntax is a constructor like any other, but is using a argument named. By coincidence the parameter has the same name as the property, but make no mistake, there is the name of the parameter, and not the property. The most suitable 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 even better when some have default values. So it is possible to use the arguments in the order you want and it is easier to omit the ones that already have values.
This syntax is totally dependent on signature of the manufacturer.
Difference between parameter and argument.
Note that the syntax may be right. It’s bad, it’s confusing, but it could be doing a attribution of "New Student"
for a variable called StudentName
and using the value of this variable as constructor argument.
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Much of this can be avoided with the fields, or even the properties already initialized in class.
Important to note that C# 9 presented news in the subject and constructors will be a little less used in certain scenarios.
The syntax for constructs with named arguments is
var stud = new Student(studentName: "New Student");
.– Thiago Lunardi
Matheus, I advise you to read the news of c# 6.0. https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6
– Pablo Tondolo de Vargas