C#6 新增特性目录
1 internal class Person
2 {
3 public string Name { get; private set; }
4 public int Age { get; private set; }
5
6 public Person(string name,int age)
7 {
8 Name = name;
9 Age = age;
10 }
11 }
通常情况下,C#的属性可以很好的帮助我们完成工作,比如上面的代码。在为属性赋值的时候,我们可以在任意地方为其赋值。但是并没有一种像是字段一样的声明且立即初始化的语法来简化默认值的设定。C#6为我们带来了这种新的语法,像是为字段赋值一样为属性赋值。
我们也知道,C#的属性实际上是一个编译器自动生成的私有字段、get_xxx和set_xxx、一条元数据组成,比如上面的代码编译后:
<Name>k__BackingField字段的IL
1 .field private string '<Name>k__BackingField'
2 .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
3 .custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = ( 01 00 00 00 00 00 00 00 )
表示一个私有字段,第2行分别表示这个自动是编译器自动生成的,第3行表示该字段不显示在Debugger窗口中。
get_Name方法的IL:
1 .method public hidebysig specialname instance string
2 get_Name() cil managed
3 {
4 .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
5 // Code size 7 (0x7)
6 .maxstack 8
7 IL_0000: ldarg.0
8 IL_0001: ldfld string csharp6.Person::'<Name>k__BackingField'
9 IL_0006: ret
10 } // end of method Person::get_Name
这也是一个自动生成的方法。
set_Name方法的IL:
1 .method private hidebysig specialname instance void
2 set_Name(string 'value') cil managed
3 {
4 .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
5 // Code size 8 (0x8)
6 .maxstack 8
7 IL_0000: ldarg.0
8 IL_0001: ldarg.1
9 IL_0002: stfld string csharp6.Person::'<Name>k__BackingField'
10 IL_0007: ret
11 } // end of method Person::set_Name
同样是一个自动生成的方法。
Name属性的IL:
1 .property instance string Name()
2 {
3 .get instance string csharp6.Person::get_Name()
4 .set instance void csharp6.Person::set_Name(string)
5 } // end of property Person::Name
表示Name属性由一个get方法和set方法组成。
1 internal class Person
2 {
3 //声明读写属性、且初始化默认值
4 public string Name { get; set; } = "blackheart";
5
6 //声明只读属性、且初始化默认值
7 public int Age { get; } = 1;
8
9 //声明只读属性
10 public string Note { get; }
11
12 public Person(string note)
13 {
14 //在构造器中为只读属性初始化默认值
15 Note = note;
16 }
17
18 private void func1()
19 {
20 //error,只能在构造器中初始化
21 //Note = "123";
22 //Age = 1;
23 //可以修改,因为有set访问器
24 Name = "new name";
25 }
26 }
这种新语法会在没有set访问器的时候把隐藏的私有字段设置为只读字段(readonly ),只允许在声明的时候设置初始值或者在构造器里面赋值。看看IL:
只有Name属性具有set_Name方法,而Age和Note属性则没有set访问器,且对应的私有字段被设置为"initonly",表示这是一个只读字段。
构造器方法,Name{get;set;}="blackheart"和Age{get;}=1的初始化操作部分被转移到实例构造函数".ctor"方法中。
1 .method public hidebysig specialname rtspecialname
2 instance void .ctor(string note) cil managed
3 {
4 // Code size 34 (0x22)
5 .maxstack 8
6 IL_0000: ldarg.0
7 IL_0001: ldstr "blackheart"
8 IL_0006: stfld string csharp6.Person::'<Name>k__BackingField'
9 IL_000b: ldarg.0
10 IL_000c: ldc.i4.1
11 IL_000d: stfld int32 csharp6.Person::'<Age>k__BackingField'
12 IL_0012: ldarg.0
13 IL_0013: call instance void [mscorlib]System.Object::.ctor()
14 IL_0018: nop
15 IL_0019: nop
16 IL_001a: ldarg.0
17 IL_001b: ldarg.1
18 IL_001c: stfld string csharp6.Person::'<Note>k__BackingField'
19 IL_0021: ret
20 } // end of method Person::.ctor
和之前的语法生成的代码可以说是一致的,均是生成为一个字段、get_xxx和set_xxx方法和对应的属性元数据,本质依然是编译器的语法简化。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有