[C#6] 2-nameof 运算符

0. 目录

C#6 新增特性目录

1. 老版本的代码

 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 }

这段代码并没什么问题,运行良好。随着时间的推移,有一天,我觉得args这个参数名不合适,想改一个更直观的名字filePaths,表示我要接受一个文件路径的数组。然后我们就直接把args这个名字给重构了,but,把 throw new ArgumentNullException("args"); 给忘了(resharper重构可能会同时重构这个名字),因为它仅仅是个字符串,书写的时候容易拼错,重构的时候也无法对它进行一个是否需要重构的分析,导致一些麻烦事情。

那么nameof运算符的目的就是来解决这个问题的。

2. nameof 运算符

nameof是C#6新增的一个关键字运算符,主要作用是方便获取类型、成员和变量的简单字符串名称(非完全限定名),意义在于避免我们在代码中写下固定的一些字符串,这些固定的字符串在后续维护代码时是一个很繁琐的事情。比如上面的代码改写后:

 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 }

我们把固定的 "args" 替换成等价的 nameof(args) 。按照惯例,贴出来两种方式的代码的IL。

"args"方式的IL代码:

 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)方式的IL代码:

 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

一样一样的,我是没看出来有任何的差异,,,so,这个运算符也是一个编译器层面提供的语法糖,编译后就没有nameof的影子了。

3. nameof 注意事项

nameof可以用于获取具名表达式的当前名字简单字符串表示(非完全限定名)。注意当前名字这个限定,比如下面这个例子,你觉得会输出什么结果?

 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 }

第一个语句输出"CC",因为它是当前的名字,虽然是指向System.ConsoleColor枚举的别名,但是由于CC是当前的名字,那么nameof运算符的结果就是"CC"。

第二个语句输出了"ConsoleColor",因为它是System.ConsoleColor的简单字符串表示,而非取得它的完全限定名,如果想取得"System.ConsoleColor",那么请使用 typeof(System.ConsoleColor).FullName 。再比如微软给的例子: nameof(person.Address.ZipCode) ,结果是"ZipCode"。

4. 参考

C#语言参考-运算符:nameof

posted @ 2016-07-03 00:34  Timetombs  阅读(19569)  评论(1编辑  收藏  举报