1. 老版本的代码
namespace csharp6
{
internal class Person
{
public string Name { get; set; }
}
internal class Program
{
private static void Main()
{
Person person = null;
string name = null;
if (person != null)
{
name = person.Name;
}
}
}
}
在我们使用一个对象的属性的时候,有时候第一步需要做的事情是先判断这个对象本身是不是bull,不然的话你可能会得到一个System.NullReferenceException 的异常。虽然有时候我们可以使用三元运算符string name = person != null ? person.Name : null;来简化代码,但是这种书写方式还是不够简单......由于null值检测时编程中非常常用的一种编码行为,so,C#6为我们带来了一种更为简化的方式。
2. null条件运算符
namespace csharp6
{
internal class Person
{
public string Name { get; set; }
}
internal class Program
{
private static void Main()
{
Person person = null;
string name = person?.Name;
}
}
}
从上面我们可以看出,使用?. 这种方式可以代替if判断和简化三元运算符的使用,简洁到不能再简洁了吧。按照惯例,上两份IL代码对比对比。
老版本的IL代码:
.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size 23 (0x17)
.maxstack 2
.locals init ([0] class csharp6.Person person,
[1] string name,
[2] bool V_2)
IL_0000: nop
IL_0001: ldnull
IL_0002: stloc.0
IL_0003: ldnull
IL_0004: stloc.1
IL_0005: ldloc.0
IL_0006: ldnull
IL_0007: cgt.un
IL_0009: stloc.2
IL_000a: ldloc.2
IL_000b: brfalse.s IL_0016
IL_000d: nop
IL_000e: ldloc.0
IL_000f: callvirt instance string csharp6.Person::get_Name()
IL_0014: stloc.1
IL_0015: nop
IL_0016: ret
} // end of method Program::Main
if版的IL
新语法的IL:
.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size 17 (0x11)
.maxstack 1
.locals init ([0] class csharp6.Person person,
[1] string name)
IL_0000: nop
IL_0001: ldnull
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: brtrue.s IL_0009
IL_0006: ldnull
IL_0007: br.s IL_000f
IL_0009: ldloc.0
IL_000a: call instance string csharp6.Person::get_Name()
IL_000f: stloc.1
IL_0010: ret
} // end of method Program::Main
null条件运算符版的IL
咦,貌似有很大不一样,我们再来一份三元运算符版的IL看看:
.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size 17 (0x11)
.maxstack 1
.locals init ([0] class csharp6.Person person,
[1] string name)
IL_0000: nop
IL_0001: ldnull
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: brtrue.s IL_0009
IL_0006: ldnull
IL_0007: br.s IL_000f
IL_0009: ldloc.0
IL_000a: callvirt instance string csharp6.Person::get_Name()
IL_000f: stloc.1
IL_0010: ret
} // end of method Program::Main










