[C #6] 4-string interpolation,
0. Directory
C #6 Add feature catalog
1. Old Version code
1 internal class Person 2 { 3 public string Name { get; set; } 4 public int Age { get; set; } 5 6 public override string ToString() 7 { 8 return string.Format("[name={0},age={1}]", Name, Age); 9 }10 }
Generally, when formatting a string, we use the static string Format method to concatenate the string, and then use {0}... {n} to act as a placeholder. If {n} is too large, the Code's readability will drop sharply. C #6 introduces a new syntax to simplify this writing method.
2. $ "{xxx :}"
Let's look at the new syntax:
1 internal class Person 2 { 3 public string Name { get; set; } 4 public int Age { get; set; } 5 6 public override string ToString() 7 { 8 return $"[name={Name},age={Age}]"; 9 }10 }
The new syntax starts with $, and then replaces {n} with a meaningful expression, which is intuitive and concise, and contains smart prompts in VS2015. Well, the basic usage is like this. Compare the IL code according to old habits.
Earlier versions of IL:
1 .method public hidebysig virtual instance string 2 ToString() cil managed 3 { 4 // Code size 33 (0x21) 5 .maxstack 3 6 .locals init ([0] string V_0) 7 IL_0000: nop 8 IL_0001: ldstr "[name={0},age={1}]" 9 IL_0006: ldarg.010 IL_0007: call instance string csharp6.Person::get_Name()11 IL_000c: ldarg.012 IL_000d: call instance int32 csharp6.Person::get_Age()13 IL_0012: box [mscorlib]System.Int3214 IL_0017: call string [mscorlib]System.String::Format(string,15 object,16 object)17 IL_001c: stloc.018 IL_001d: br.s IL_001f19 IL_001f: ldloc.020 IL_0020: ret21 } // end of method Person::ToString
New syntax IL:
1 .method public hidebysig virtual instance string 2 ToString() cil managed 3 { 4 // Code size 33 (0x21) 5 .maxstack 3 6 .locals init ([0] string V_0) 7 IL_0000: nop 8 IL_0001: ldstr "[name={0},age={1}]" 9 IL_0006: ldarg.010 IL_0007: call instance string csharp6.Person::get_Name()11 IL_000c: ldarg.012 IL_000d: call instance int32 csharp6.Person::get_Age()13 IL_0012: box [mscorlib]System.Int3214 IL_0017: call string [mscorlib]System.String::Format(string,15 object,16 object)17 IL_001c: stloc.018 IL_001d: br.s IL_001f19 IL_001f: ldloc.020 IL_0020: ret21 } // end of method Person::ToString
At first glance, I saw the new version of IL code. I thought I didn't recompile my code. C # The Compiler helps us convert it into an old version, which is exactly the same... So, which is also a syntactic optimization.
3. Example
1 // supports method call 2 string s1 =$ "{person. getHashCode ()} "; 3 // supported expression 4 string s2 = $" person. {nameof (person. name)} is {person ?. Name} "; 5 // supports formatting and outputting 6 DateTime now = DateTime. now; 7 string s3 = $ "DateTime. now = {now: yyyy-MM-dd HH: mm: ss} "; 8 // combined expression and formatting output 9 string s4 =$" {person. name, 2} is {person. age: D2} year {(person. age = 1? "": "S")} old. "; 10 // supports implicit conversions of 11 IFormattable s5 = $" Hello, {person. name} "; 12 FormattableString s6 = $" Hello, {person. name }"
The new syntax supports expression evaluation, formatting, and implicit conversion to IFormattable. The compilation result is System. runtime. compilerServices. formattableStringFactory. create this static method to construct a FormattableString implementation. IL:
1 IL_0095: stloc.s s4 2 IL_0097: ldstr "Hello, {0}" 3 IL_009c: ldc.i4.1 4 IL_009d: newarr[mscorlib] System.Object 5 IL_00a2: dup 6 IL_00a3: ldc.i4.0 7 IL_00a4: ldloc.0 8 IL_00a5: callvirt instance string csharp6.Person::get_Name() 9 IL_00aa: stelem.ref10 IL_00ab: call class [mscorlib]System.FormattableString[mscorlib] System.Runtime.CompilerServices.FormattableStringFactory::Create(string,object[])13 IL_00b0: stloc.s s514 IL_00b2: ldstr "Hello, {0}"15 IL_00b7: ldc.i4.116 IL_00b8: newarr[mscorlib] System.Object17 IL_00bd: dup18 IL_00be: ldc.i4.019 IL_00bf: ldloc.020 IL_00c0: callvirt instance string csharp6.Person::get_Name()21 IL_00c5: stelem.ref22 IL_00c6: call class [mscorlib]System.FormattableString[mscorlib] System.Runtime.CompilerServices.FormattableStringFactory::Create(string,object[])
4. Reference
Interpolated Strings