c#3.0系列:Object Initializer 和 Collection Initializer
对象初始化器(Object Initializers) 和 集合初始化器(Collection Initializers)
,就是简化我们的代码,让本来几行才能写完的代码一行写完。这样在LINQ的使用中,我们才不会把一个LINQ表达式写的复杂。
Object Initializers
Object Initializer的使用很简单:在通过new 关键字创建对象的时候,将所需的Proeprty置于Type name后的{}中。比如:
注意:
在使用对象初始化器时,隐式的调用了默认的构造函数(例如S2的创建及初始化)。
在使用对象初始化器时,也可以指定调用任何自定义的构造函数。(例如S1,S3的创建及初始化)。
可以一次为多个(不一定要全部)属性赋值,
调用对应Class的对应的Constructor创建一个临时对象。
Object Initializers
Object Initializer的使用很简单:在通过new 关键字创建对象的时候,将所需的Proeprty置于Type name后的{}中。比如:
1class staff
2 {
3 public string Name { get; set; }
4 public int Age{get; set;}
5 public string Add;
6 public staff()
7 { }
8 public staff(int i, string s)
9 {
10 Name = s;
11 Age = i;
12 }
13 }
使用对象初始化器 2 {
3 public string Name { get; set; }
4 public int Age{get; set;}
5 public string Add;
6 public staff()
7 { }
8 public staff(int i, string s)
9 {
10 Name = s;
11 Age = i;
12 }
13 }
1Console.WriteLine("Initializer");
2 staff s1 = new staff(12, "sss") { Name = "GUOJUN" };
3 staff s3 = new staff(12, "sss") { Name = "GUOJUN", Age = 27,Add="HuBei" };
4 staff s2 = new staff { Name = "IORI", Age = 27 };
在上面的例子中,我们通过一句代码(staff s1 = new staff(12, "sss") { Name = "GUOJUN" };)实现对staff 对象的创建和初始化。2 staff s1 = new staff(12, "sss") { Name = "GUOJUN" };
3 staff s3 = new staff(12, "sss") { Name = "GUOJUN", Age = 27,Add="HuBei" };
4 staff s2 = new staff { Name = "IORI", Age = 27 };
注意:
在使用对象初始化器时,隐式的调用了默认的构造函数(例如S2的创建及初始化)。
在使用对象初始化器时,也可以指定调用任何自定义的构造函数。(例如S1,S3的创建及初始化)。
可以一次为多个(不一定要全部)属性赋值,
1staff s1 = new staff(12, "sss") { Name = "GUOJUN" };
2staff s3 = new staff(12, "sss") { Name = "GUOJUN", Age = 27,Add="HuBei" };
C# 3.x这些Feature仅仅是基于一种Programming Language层面的新特性而已,这些特性通过Programming Language对应的Compiler在编译过程添加一些辅助的Code来实现。 通过编译我们看看变成什么样子:2staff s3 = new staff(12, "sss") { Name = "GUOJUN", Age = 27,Add="HuBei" };
1public static void fnInitializer()
2{
3 Console.WriteLine("Initializer");
4 staff <>g__initLocal3 = new staff(12, "sss");
5 <>g__initLocal3.Name = "GUOJUN";
6 staff s1 = <>g__initLocal3;
7 staff <>g__initLocal4 = new staff(12, "sss");
8 <>g__initLocal4.Name = "GUOJUN";
9 <>g__initLocal4.Age = 0x1b;
10 <>g__initLocal4.Add = "HuBei";
11 staff s3 = <>g__initLocal4;
12 staff <>g__initLocal5 = new staff();
13 <>g__initLocal5.Name = "IORI";
14 <>g__initLocal5.Age = 0x1b;
15 staff s2 = <>g__initLocal5;
16 Console.WriteLine(s1.Name);
17 Console.WriteLine(s1.Age);
18 Console.WriteLine(s2.Name);
19}
我想上面的代码就不用我太多的说明,大家一定明白。我只说明一点:
2{
3 Console.WriteLine("Initializer");
4 staff <>g__initLocal3 = new staff(12, "sss");
5 <>g__initLocal3.Name = "GUOJUN";
6 staff s1 = <>g__initLocal3;
7 staff <>g__initLocal4 = new staff(12, "sss");
8 <>g__initLocal4.Name = "GUOJUN";
9 <>g__initLocal4.Age = 0x1b;
10 <>g__initLocal4.Add = "HuBei";
11 staff s3 = <>g__initLocal4;
12 staff <>g__initLocal5 = new staff();
13 <>g__initLocal5.Name = "IORI";
14 <>g__initLocal5.Age = 0x1b;
15 staff s2 = <>g__initLocal5;
16 Console.WriteLine(s1.Name);
17 Console.WriteLine(s1.Age);
18 Console.WriteLine(s2.Name);
19}
Collection Initializer
使用集合初始化器,如下:
1 List<staff> myListOfstaff = new List<staff>
2 {
3 new staff {Name = "GUOJUN", Age = 27 },
4 new staff {Name = "IORI", Age = 27 },
5 new staff(12, "sss") { Name = "GUOJUN" }
6 };
7 foreach (staff pt in myListOfstaff)
8 {
9 Console.WriteLine(pt);
10 }
分析Object Initializer的本质一样,我们之后看看通过Compiler变异后的Code是什么样子,就会对Collection Initializer的实现有一个全面的了解: 2 {
3 new staff {Name = "GUOJUN", Age = 27 },
4 new staff {Name = "IORI", Age = 27 },
5 new staff(12, "sss") { Name = "GUOJUN" }
6 };
7 foreach (staff pt in myListOfstaff)
8 {
9 Console.WriteLine(pt);
10 }
1 List<staff> <>g__initLocal6 = new List<staff>();
2 staff <>g__initLocal7 = new staff();
3 <>g__initLocal7.Name = "GUOJUN";
4 <>g__initLocal7.Age = 0x1b;
5 <>g__initLocal6.Add(<>g__initLocal7);
6 staff <>g__initLocal8 = new staff();
7 <>g__initLocal8.Name = "IORI";
8 <>g__initLocal8.Age = 0x1b;
9 <>g__initLocal6.Add(<>g__initLocal8);
10 staff <>g__initLocal9 = new staff(12, "sss");
11 <>g__initLocal9.Name = "GUOJUN";
12 <>g__initLocal6.Add(<>g__initLocal9);
13 List<staff> myListOfstaff = <>g__initLocal6;
14 foreach (staff pt in myListOfstaff)
15 {
16 Console.WriteLine(pt);
17 }
18
Collection Initializer的实现和Object Initializer很类似。
2 staff <>g__initLocal7 = new staff();
3 <>g__initLocal7.Name = "GUOJUN";
4 <>g__initLocal7.Age = 0x1b;
5 <>g__initLocal6.Add(<>g__initLocal7);
6 staff <>g__initLocal8 = new staff();
7 <>g__initLocal8.Name = "IORI";
8 <>g__initLocal8.Age = 0x1b;
9 <>g__initLocal6.Add(<>g__initLocal8);
10 staff <>g__initLocal9 = new staff(12, "sss");
11 <>g__initLocal9.Name = "GUOJUN";
12 <>g__initLocal6.Add(<>g__initLocal9);
13 List<staff> myListOfstaff = <>g__initLocal6;
14 foreach (staff pt in myListOfstaff)
15 {
16 Console.WriteLine(pt);
17 }
18
版权所有归"布衣软件工作者".未经容许不得转载.