0. Catalogue
C#6 new features Directory
1. Old version of the code
1 Internal class Person2 {3 Public stringName {Get;Set; }4 Public intAge {Get;Set; }5 6 Public Override stringToString ()7 {8 return string. Format ("[Name={0},age={1}]", Name, age);9 }Ten}
Usually, when we format a string, we use string's static method format for string concatenation, and then use {0} ... {n} to act as a placeholder. If {n} is too large, the readability of the code is drastically reduced, and C#6 introduces a new syntax to simplify this writing method.
2. $ "{XXX:}"
Let's look at the new syntax between us:
1 Internal class Person2 {3 Public stringName {Get;Set; }4 Public intAge {Get;Set; }5 6 Public Override stringToString ()7 {8 return$"[Name={name},age={age}]";9 }Ten}
The new syntax starts with $ and then replaces the previous {n} with a meaningful expression, intuitive and concise, with smart hints in VS2015. OK, the basic usage is this, according to the old habit, compare the IL code bar.
Old version of IL:
1. method PublicHidebysigVirtualInstancestring 2 ToString () cil managed3 {4 //Code Size (0x21)5. maxstack36. Locals init ([0]stringv_0)7 Il_0000:nop8Il_0001:ldstr"[Name={0},age={1}]"9Il_0006:ldarg.0TenIl_0007:call instancestringCSHARP6. Person::get_name () OneIl_000c:ldarg.0 A Il_000d:call instance int32 csharp6. Person::get_age () - Il_0012:box [Mscorlib]system.int32 -Il_0017:callstring[mscorlib] System.string::format (string, the Object, - Object) -Il_001c:stloc.0 - IL_001D:BR.S il_001f +Il_001f:ldloc.0 - Il_0020:ret +}//end of Method person::tostring
Il of the new syntax:
1. method PublicHidebysigVirtualInstancestring 2 ToString () cil managed3 {4 //Code Size (0x21)5. maxstack36. Locals init ([0]stringv_0)7 Il_0000:nop8Il_0001:ldstr"[Name={0},age={1}]"9Il_0006:ldarg.0TenIl_0007:call instancestringCSHARP6. Person::get_name () OneIl_000c:ldarg.0 A Il_000d:call instance int32 csharp6. Person::get_age () - Il_0012:box [Mscorlib]system.int32 -Il_0017:callstring[mscorlib] System.string::format (string, the Object, - Object) -Il_001c:stloc.0 - IL_001D:BR.S il_001f +Il_001f:ldloc.0 - Il_0020:ret +}//end of Method person::tostring
At first sight of the new version of IL code, I thought I hadn't recompiled my code. The C # compiler helped us turn the old version into the same ... So, this is again a grammatical level of optimization.
3. Example
1 //support for method calls2 stringS1 = $"{person. GetHashCode ()}";3 //Support Expressions4 stringS2 = $"Person . {nameof (person. Name)} is {person?. Name}";5 //support for formatted output6DateTime now =DateTime.Now;7 stringS3 = $"datetime.now={now:yyyy-mm-dd HH:mm:ss}";8 //combining expressions and formatted output9 stringS4 = $"{person. name,2} is {person. AGE:D2} year{(person. Age = = 1? "" : "S")} old.";Ten //supported implicit type conversions OneIFormattable S5 = $"Hello, {person. Name}"; AFormattablestring s6 = $"Hello, {person. Name}"
The new syntax supports expression evaluation, supports: formatting operations, and also supports implicit conversions to IFormattable, which are compiled using System.Runtime.CompilerServices.FormattableStringFactory.Create This static method constructs a formattablestring implementation. Il is as follows:
1 il_0095: Stloc.sS42 il_0097: ldstr "Hello, {0}"3 il_009c: ldc.i4.14 il_009d: Newarr[mscorlib] System.Object5 IL_00A2: DUP6 IL_00A3: ldc.i4.07 IL_00A4: ldloc.08 il_00a5: callvirt instance stringCSHARP6. Person::get_name ()9 IL_00AA: stelem.refTen Il_00ab: Pager class[mscorlib]System.formattablestring[mscorlib] System.runtime.compilerservices.formattablestringfactory::create (string,Object[]) - il_00b0: Stloc.sS5 - il_00b2: ldstr "Hello, {0}" the 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 stringCSHARP6. Person::get_name () + IL_00C5: stelem.ref A il_00c6: Pager class[mscorlib]System.formattablestring[mscorlib] System.runtime.compilerservices.formattablestringfactory::create (string,Object[])
4. Reference
Interpolated Strings
[C#6] 4-string interpolation