Object initializer
The object initializer simplifies the code and completes the code written in just a few lines.
You can assign values to any accessible fields or attributes of an object when creating an object without displaying the call constructor, in fact, the biggest role of the Object initializer is to reduce the amount of code writing and hand over what was originally done by some people to the framework.
Example
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace _ 13_ObjectInitializers
{
Class Program
{
Static void Main (string [] args)
{
Student stu = new Student {_ name = "Xiaoqiang", Sex = "male", _ age = 25 };
Console. WriteLine (stu. _ name );
Console. WriteLine (stu. Sex );
Console. WriteLine (stu. _ age );
Console. ReadKey ();
}
}
Class Student
{
Public string _ name;
Public string Sex {get; set ;}
Public int _ age;
}
}
Running Effect