[C #6] 2-nameof operator,
0. Directory
C #6 Add feature catalog
1. Old Version 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 does not have any problems and runs well. As time went by, one day, I thought the args parameter name was inappropriate. I wanted to change the more intuitive name filePaths to accept an array of file paths. Then we will refactor The args name directly, but, and throw new ArgumentNullException ("args"); forget (resharper refactoring may refactor this name at the same time ), because it is only a string, it is prone to spelling errors during writing, and it cannot be analyzed to determine whether to refactor it, causing some trouble.
The nameof operator aims to solve this problem.
2. nameof Operator
Nameof is a New Keyword operator in C #6. It is mainly used to conveniently obtain types, members, and variables.Simple string name(Not fully qualified), it means to avoid writing fixed strings in the Code. These fixed strings are a very tedious task to maintain code in the future. For example, after the above Code is rewritten:
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 replace the fixed "args" with the equivalent nameof (args ). By convention, the IL of the Code is pasted out in two ways.
IL code in the "args" Mode:
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: ldnull10 IL_0003: ceq11 IL_0005: stloc.012 IL_0006: ldloc.013 IL_0007: brfalse.s IL_001514 IL_0009: nop15 IL_000a: ldstr "args"16 IL_000f: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string)17 IL_0014: throw18 IL_0015: ret19 } // end of method Program::Main
IL code in nameof (args) mode:
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: ldnull10 IL_0003: ceq11 IL_0005: stloc.012 IL_0006: ldloc.013 IL_0007: brfalse.s IL_001514 IL_0009: nop15 IL_000a: ldstr "args"16 IL_000f: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string)17 IL_0014: throw18 IL_0015: ret19 } // end of method Program::Main
The same is true. I didn't see any differences. The so operator is also a syntactic sugar provided by the compiler. After compilation, there is no nameof shadow.
3. nameof considerations
Nameof can be used to obtain the name expressionCurrent nameOfSimple string(Not a fully qualified name ). Pay attention to the limitation of the current name. In the following example, what results do you think will be output?
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));//CC11 WriteLine(nameof(System.ConsoleColor));//ConsoleColor12 }13 }14 }
The first statement outputs "CC" because it is the current name, although it is directed to System. the alias of ConsoleColor enumeration. However, since CC is the current name, the nameof operator is "CC ".
The second statement outputs "lelecolor" because it is System. the simple string representation of ConsoleColor, instead of getting its fully qualified name. If you want to get "System. consoleColor ", use typeof (System. consoleColor ). fullName. For another example, the result of nameof (person. Address. ZipCode) is "ZipCode ".
4. Reference
C # Language Reference-OPERATOR: nameof