0. 目录
1. 老版本的代码
internal class Person
{
public string Name { get; set; }
public int Age { get; set; } public override string ToString()
{
return string.Format("[name={0},age={1}]", Name, Age);
}
}
通常我们在格式化字符串的时候会使用string的静态方法Format来进行字符串拼接,然后使用{0}...{n}来充当占位符。如果{n}过大,代码的可读性就会急剧下降,C#6引入一个新语法来简化这种书写方式。
2. $"{xxx:}"
我们之间来看新语法:
internal class Person
{
public string Name { get; set; }
public int Age { get; set; } public override string ToString()
{
return $"[name={Name},age={Age}]";
}
}
新语法采用 $ 开头,然后把之前的{n}换成了有意义的表达式,直观且简洁,且在VS2015中会有智能提示。好了,基本用法就是这样,按*惯,对比下IL代码吧。
老版本的IL:
.method public hidebysig virtual instance string
ToString() cil managed
{
// Code size 33 (0x21)
.maxstack
.locals init ([] string V_0)
IL_0000: nop
IL_0001: ldstr "[name={0},age={1}]"
IL_0006: ldarg.
IL_0007: call instance string csharp6.Person::get_Name()
IL_000c: ldarg.
IL_000d: call instance int32 csharp6.Person::get_Age()
IL_0012: box [mscorlib]System.Int32
IL_0017: call string [mscorlib]System.String::Format(string,
object,
object)
IL_001c: stloc.
IL_001d: br.s IL_001f
IL_001f: ldloc.
IL_0020: ret
} // end of method Person::ToString
新语法的IL:
.method public hidebysig virtual instance string
ToString() cil managed
{
// Code size 33 (0x21)
.maxstack
.locals init ([] string V_0)
IL_0000: nop
IL_0001: ldstr "[name={0},age={1}]"
IL_0006: ldarg.
IL_0007: call instance string csharp6.Person::get_Name()
IL_000c: ldarg.
IL_000d: call instance int32 csharp6.Person::get_Age()
IL_0012: box [mscorlib]System.Int32
IL_0017: call string [mscorlib]System.String::Format(string,
object,
object)
IL_001c: stloc.
IL_001d: br.s IL_001f
IL_001f: ldloc.
IL_0020: ret
} // end of method Person::ToString
第一眼看到新版本的IL代码,我还以为我没有重新编译我的代码。C#编译器帮我们转成了老版本的写法而已,一模一样的。。。so,这又是一个语法层面的优化。
3. Example
//支持方法调用
string s1 = $"{person.GetHashCode()}";
//支持表达式
string s2 = $"person.{nameof(person.Name)} is {person?.Name}";
//支持格式化输出
DateTime now = DateTime.Now;
string s3 = $"DateTime.Now={now:yyyy-MM-dd HH:mm:ss}";
//组合表达式和格式化输出
string s4 = $"{person.Name,2} is {person.Age:D2} year{(person.Age == 1 ? "" : "s")} old.";
//支持的隐式类型转换
IFormattable s5 = $"Hello, {person.Name}";
FormattableString s6 = $"Hello, {person.Name}"
新语法支持表达式求值,支持:格式化操作,还支持到IFormattable的隐式转换,编译结果是利用 System.Runtime.CompilerServices.FormattableStringFactory.Create 这个静态方法构造一个 FormattableString 实现的。IL如下:
IL_0095: stloc.s s4
IL_0097: ldstr "Hello, {0}"
IL_009c: ldc.i4.1
IL_009d: newarr[mscorlib] System.Object
IL_00a2: dup
IL_00a3: ldc.i4.0
IL_00a4: ldloc.0
IL_00a5: callvirt instance string csharp6.Person::get_Name()
IL_00aa: stelem.ref
IL_00ab: call class [mscorlib]System.FormattableString[mscorlib] System.Runtime.CompilerServices.FormattableStringFactory::Create(string,object[])
IL_00b0: stloc.s s5
IL_00b2: ldstr "Hello, {0}"
IL_00b7: ldc.i4.1
IL_00b8: newarr[mscorlib] System.Object
IL_00bd: dup
IL_00be: ldc.i4.0
IL_00bf: ldloc.0
IL_00c0: callvirt instance string csharp6.Person::get_Name()
IL_00c5: stelem.ref
IL_00c6: call class [mscorlib]System.FormattableString[mscorlib] System.Runtime.CompilerServices.FormattableStringFactory::Create(string,object[])