C# 对象初始化器
C#3.0(.NET 3.5)引入了对象初始化器语法,这是一种初始化类或集合对象的新方法。对象初始化程序允许您在创建对象时将值分配给字段或属性,而无需调用构造函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> { <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> StudentID { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> StudentName { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> Age { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Address { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } } <span class="hljs-keyword">class</span> <span class="hljs-title">Program</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>)</span> { Student std = <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">1</span>, StudentName = <span class="hljs-string">"Bill"</span>, Age = <span class="hljs-number">20</span>, Address = <span class="hljs-string">"New York"</span> }; } } |
在上面的示例中,没有任何构造函数的情况下定义了 Student 类。在 Main() 方法中,我们创建了Student对象,并同时为大括号中的所有或某些属性分配了值。这称为对象初始化器语法。
编译器将上述初始化程序编译为如下所示的内容。
1 2 3 4 5 6 7 8 |
Student __student = <span class="hljs-keyword">new</span> Student(); __student.StudentID = <span class="hljs-number">1</span>; __student.StudentName = <span class="hljs-string">"Bill"</span>; __student.Age = <span class="hljs-number">20</span>; __student.StandardID = <span class="hljs-number">10</span>; __student.Address = <span class="hljs-string">"Test"</span>; Student std = __student; |
集合初始化器语法
可以使用集合初始化器语法以与类对象相同的方式初始化集合。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="hljs-keyword">var</span> student1 = <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">1</span>, StudentName = <span class="hljs-string">"John"</span> }; <span class="hljs-keyword">var</span> student2 = <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">2</span>, StudentName = <span class="hljs-string">"Steve"</span> }; <span class="hljs-keyword">var</span> student3 = <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">3</span>, StudentName = <span class="hljs-string">"Bill"</span> } ; <span class="hljs-keyword">var</span> student4 = <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">3</span>, StudentName = <span class="hljs-string">"Bill"</span> }; <span class="hljs-keyword">var</span> student5 = <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">5</span>, StudentName = <span class="hljs-string">"Ron"</span> }; IList<Student> studentList = <span class="hljs-keyword">new</span> List<Student>() { student1, student2, student3, student4, student5 }; |
您还可以同时初始化集合和对象。
1 2 3 4 5 6 7 8 |
IList<Student> studentList = <span class="hljs-keyword">new</span> List<Student>() { <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">1</span>, StudentName = <span class="hljs-string">"John"</span>} , <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">2</span>, StudentName = <span class="hljs-string">"Steve"</span>} , <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">3</span>, StudentName = <span class="hljs-string">"Bill"</span>} , <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">3</span>, StudentName = <span class="hljs-string">"Bill"</span>} , <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">4</span>, StudentName = <span class="hljs-string">"Ram"</span> } , <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">5</span>, StudentName = <span class="hljs-string">"Ron"</span> } }; |
您还可以将null指定为元素:
1 2 3 4 |
IList<Student> studentList = <span class="hljs-keyword">new</span> List<Student>() { <span class="hljs-keyword">new</span> Student() { StudentID = <span class="hljs-number">1</span>, StudentName = <span class="hljs-string">"John"</span>} , <span class="hljs-literal">null</span> }; |
初始化器的优点
- 初始化程序语法使代码更具可读性,易于将元素添加到集合中。
- 在多线程中很有用。