0. Contents
1. The old version of the code
1 using System; 2 namespace csharp6 3 { 4 internal class Program 5 { 6 private static void Main(string[] args) 7 { 8 if (args==null) 9 { 10 throw new ArgumentNullException("args"); 11 } 12 } 13 } 14 }
This code is no problem, good run. Over time, one day, I think this args parameter name is inappropriate, want to change the name of a more intuitive filePaths, it means I have to accept an array of file paths. Then we just put the name args to reconstruct, but, to throw new ArgumentNullException ( "args") ; to forget (resharper while reconstruction may reconstruct the name) because it is just a string, writing when easily misspelled, reconstruction, they also can not analyze it a need for reconstruction, leading to some trouble to do.
Then the purpose nameof operator is to solve this problem.
2. nameof operator
nameof is a new C # 6 key operator, the main role is to facilitate access type, and member variables of a simple string name (not fully qualified name), meaning that we avoid write down some fixed string in the code these strings immobilized during subsequent maintenance code is a very complicated matter. After rewriting the code example above:
1 using System; 2 namespace csharp6 3 { 4 internal class Program 5 { 6 private static void Main(string[] args) 7 { 8 if (args==null) 9 { 10 throw new ArgumentNullException(nameof(args)); 11 } 12 } 13 } 14 }
We fixed the "args" replaced with equivalent nameof (args). Conventionally, the IL code posted in two ways.
"Args" approach IL code:
1 .method private hidebysig static void Main(string[] args) cil managed 2 { 3 .entrypoint 4 // Code size 22 (0x16) 5 .maxstack 2 6 .locals init ([0] bool V_0) 7 IL_0000: nop 8 IL_0001: ldarg.0 9 IL_0002: ldnull 10 IL_0003: ceq 11 IL_0005: stloc.0 12 IL_0006: ldloc.0 13 IL_0007: brfalse.s IL_0015 14 IL_0009: nop 15 IL_000a: ldstr "args" 16 IL_000f: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string) 17 IL_0014: throw 18 IL_0015: ret 19 } // end of method Program::Main
nameof (args) mode IL code:
1 .method private hidebysig static void Main(string[] args) cil managed 2 { 3 .entrypoint 4 // Code size 22 (0x16) 5 .maxstack 2 6 .locals init ([0] bool V_0) 7 IL_0000: nop 8 IL_0001: ldarg.0 9 IL_0002: ldnull 10 IL_0003: ceq 11 IL_0005: stloc.0 12 IL_0006: ldloc.0 13 IL_0007: brfalse.s IL_0015 14 IL_0009: nop 15 IL_000a: ldstr "args" 16 IL_000f: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string) 17 IL_0014: throw 18 IL_0015: ret 19 } // end of method Program::Main
Like the same, I did not see any difference ,,, so, syntactic sugar this operator also provided a compiler level, after compiling without a shadow of a nameof.
3. nameof Notes
nameof named expressions can be used to obtain the current name of simple string representation (not fully qualified names). Note the name of the current limit, such as the following example, what do you think will output the results?
1 using static System.Console; 2 using CC = System.ConsoleColor; 3 4 namespace csharp6 5 { 6 internal class Program 7 { 8 private static void Main() 9 { 10 WriteLine(nameof(CC));//CC 11 WriteLine(nameof(System.ConsoleColor));//ConsoleColor 12 } 13 } 14 }
The first statement output "CC", because it is the current name, though, is the enumeration of the alias pointing to System.ConsoleColor, but because CC is the current name, then the result nameof operator is "CC".
The second statement outputs a "ConsoleColor", because it is a simple string System.ConsoleColor said - rather than its fully qualified name, if you want to get "System.ConsoleColor", then please use typeof (System.ConsoleColor). FullName. Another example is Microsoft gave an example: NameOf (person.Address.ZipCode), the result is "ZipCode".
4. Reference
0. Contents
1. The old version of the code
1 using System; 2 namespace csharp6 3 { 4 internal class Program 5 { 6 private static void Main(string[] args) 7 { 8 if (args==null) 9 { 10 throw new ArgumentNullException("args"); 11 } 12 } 13 } 14 }
This code is no problem, good run. Over time, one day, I think this args parameter name is inappropriate, want to change the name of a more intuitive filePaths, it means I have to accept an array of file paths. Then we just put the name args to reconstruct, but, to throw new ArgumentNullException ( "args") ; to forget (resharper while reconstruction may reconstruct the name) because it is just a string, writing when easily misspelled, reconstruction, they also can not analyze it a need for reconstruction, leading to some trouble to do.
Then the purpose nameof operator is to solve this problem.
2. nameof operator
nameof is a new C # 6 key operator, the main role is to facilitate access type, and member variables of a simple string name (not fully qualified name), meaning that we avoid write down some fixed string in the code these strings immobilized during subsequent maintenance code is a very complicated matter. After rewriting the code example above:
1 using System; 2 namespace csharp6 3 { 4 internal class Program 5 { 6 private static void Main(string[] args) 7 { 8 if (args==null) 9 { 10 throw new ArgumentNullException(nameof(args)); 11 } 12 } 13 } 14 }
We fixed the "args" replaced with equivalent nameof (args). Conventionally, the IL code posted in two ways.
"Args" approach IL code:
1 .method private hidebysig static void Main(string[] args) cil managed 2 { 3 .entrypoint 4 // Code size 22 (0x16) 5 .maxstack 2 6 .locals init ([0] bool V_0) 7 IL_0000: nop 8 IL_0001: ldarg.0 9 IL_0002: ldnull 10 IL_0003: ceq 11 IL_0005: stloc.0 12 IL_0006: ldloc.0 13 IL_0007: brfalse.s IL_0015 14 IL_0009: nop 15 IL_000a: ldstr "args" 16 IL_000f: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string) 17 IL_0014: throw 18 IL_0015: ret 19 } // end of method Program::Main
nameof (args) mode IL code:
1 .method private hidebysig static void Main(string[] args) cil managed 2 { 3 .entrypoint 4 // Code size 22 (0x16) 5 .maxstack 2 6 .locals init ([0] bool V_0) 7 IL_0000: nop 8 IL_0001: ldarg.0 9 IL_0002: ldnull 10 IL_0003: ceq 11 IL_0005: stloc.0 12 IL_0006: ldloc.0 13 IL_0007: brfalse.s IL_0015 14 IL_0009: nop 15 IL_000a: ldstr "args" 16 IL_000f: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string) 17 IL_0014: throw 18 IL_0015: ret 19 } // end of method Program::Main
Like the same, I did not see any difference ,,, so, syntactic sugar this operator also provided a compiler level, after compiling without a shadow of a nameof.
3. nameof Notes
nameof named expressions can be used to obtain the current name of simple string representation (not fully qualified names). Note the name of the current limit, such as the following example, what do you think will output the results?
1 using static System.Console; 2 using CC = System.ConsoleColor; 3 4 namespace csharp6 5 { 6 internal class Program 7 { 8 private static void Main() 9 { 10 WriteLine(nameof(CC));//CC 11 WriteLine(nameof(System.ConsoleColor));//ConsoleColor 12 } 13 } 14 }
The first statement output "CC", because it is the current name, though, is the enumeration of the alias pointing to System.ConsoleColor, but because CC is the current name, then the result nameof operator is "CC".
The second statement outputs a "ConsoleColor", because it is a simple string System.ConsoleColor said - rather than its fully qualified name, if you want to get "System.ConsoleColor", then please use typeof (System.ConsoleColor). FullName. Another example is Microsoft gave an example: NameOf (person.Address.ZipCode), the result is "ZipCode".