C#3.0中自动属性和对象初始化器
2009-04-24 08:25:48 来源:WEB开发网或者通过重载构造方法的方式来初始化这些属性,二者的达到的效果是相同的,但是前者使用起来方便了些,代码量减少了许多,这个过程是怎么完成的呢?其实C#本身并没有太大的变化,这些都是在语法上的一些改变,使得编写代码的时候更方便效率更高,把一些编译器可以推断出来完成的工作让编译器来做了,编译器在编译程序的时候将我们没有实现的代码替我们实现来生成IL代码的:
method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代码大小 175 (0xaf)
.maxstack 2
.locals init ([0] class CS30.Employee employee,
[1] class CS30.Employee '<>g__initLocal0')
IL_0000: nop
IL_0001: newobj instance void CS30.Employee::.ctor()
IL_0006: stloc.1
IL_0007: ldloc.1
IL_0008: ldc.i4.1
IL_0009: callvirt instance void CS30.Employee::set_Id(int32)
IL_000e: nop
IL_000f: ldloc.1
IL_0010: ldstr bytearray (DD 84 4B 4E CE 98 ) // ..KN..
IL_0015: callvirt instance void CS30.Employee::set_Name(string)
IL_001a: nop
IL_001b: ldloc.1
IL_001c: ldc.i4.s 24
IL_001e: callvirt instance void CS30.Employee::set_Age(int32)
IL_0023: nop
IL_0024: ldloc.1
IL_0025: ldstr "1984-10-21"
IL_002a: callvirt instance void CS30.Employee::set_BirthDate(string)
IL_002f: nop
IL_0030: ldloc.1
IL_0031: ldstr bytearray (37 75 ) // 7u
IL_0036: callvirt instance void CS30.Employee::set_Sex(string)
IL_003b: nop
IL_003c: ldloc.1
IL_003d: stloc.0
IL_003e: ldstr bytearray (16 7F F7 53 3B 00 7B 00 30 00 7D 00 ) // ...S;.{.0.}.
IL_0043: ldloc.0
IL_0044: callvirt instance int32 CS30.Employee::get_Id()
IL_0049: box [mscorlib]System.Int32
IL_004e: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_0053: nop
IL_0054: ldstr bytearray (D3 59 0D 54 3A 00 7B 00 30 00 7D 00 ) // .Y.T:.{.0.}.
IL_0059: ldloc.0
IL_005a: callvirt instance string CS30.Employee::get_Name()
IL_005f: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_0064: nop
IL_0065: ldstr bytearray (74 5E 84 9F 3A 00 7B 00 30 00 7D 00 ) // t^..:.{.0.}.
IL_006a: ldloc.0
IL_006b: callvirt instance int32 CS30.Employee::get_Age()
IL_0070: box [mscorlib]System.Int32
IL_0075: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_007a: nop
IL_007b: ldstr bytearray (1F 75 E5 65 3A 00 7B 00 30 00 7D 00 ) // .u.e:.{.0.}.
IL_0080: ldloc.0
IL_0081: callvirt instance string CS30.Employee::get_BirthDate()
IL_0086: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_008b: nop
IL_008c: ldstr bytearray (27 60 2B 52 3A 00 7B 00 30 00 7D 00 ) // '`+R:.{.0.}.
IL_0091: ldloc.0
IL_0092: callvirt instance string CS30.Employee::get_Sex()
IL_0097: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_009c: nop
IL_009d: ldstr bytearray (F7 8B 09 63 FB 4E 0F 61 2E 95 E7 7E ED 7E 2E 00 // ...c.N.a...~.~..
2E 00 2E 00 ) // ....
IL_00a2: call void [mscorlib]System.Console::WriteLine(string)
IL_00a7: nop
IL_00a8: call string [mscorlib]System.Console::ReadLine()
IL_00ad: pop
IL_00ae: ret
} // end of method Program::Main
3总结:
自动属性和对象初始化器都是C#3.0提供的语法级别的功能改进,是一种语法糖,是编写代码的效率更高,将一些重复性的工作交给编译器来做,但是这种改变,也增加了代码的不透明性,这点在隐式类型中体现的更为突出,增加了代码理解的难度,这些仅仅是提供给代码编写人员的一种选择,如果不喜欢也可以按照原来的方式来书写自己的代码也未尝不可。
更多精彩
赞助商链接