[C#6] 2-nameof operator

Source: Internet
Author: User
Tags mscorlib

0. Catalogue

C#6 new features Directory

1. Old version of the code
1 usingSystem;2 namespaceCSHARP63 {4     Internal class Program5     {6         Private Static voidMain (string[] args)7         {8             if(args==NULL)9             {Ten                 Throw NewArgumentNullException ("args"); One             } A         } -     } -}

This code is no problem and works well. Over time, one day, I think that the args parameter name is not appropriate, want to change a more intuitive name filepaths, indicating that I want to accept an array of file paths. Then we reconstructed the name of the args directly, but put the throw new ArgumentNullException ("args" ); Forget (ReSharper refactoring may refactor the name at the same time), because it's just a string, it's easy to misspell when it's written, and it's not possible to refactor when refactoring is an analysis that leads to some troublesome things.

Then the purpose of the nameof operator is to solve the problem.

1. nameof operator

Nameof is a new keyword operator for c#6, which is designed to facilitate the simple string names (not fully qualified names) of types, members, and variables, meaning to avoid writing fixed strings in our code. These fixed strings are a tedious thing to follow when maintaining code. For example, when the above code is rewritten:

1 usingSystem;2 namespaceCSHARP63 {4     Internal class Program5     {6         Private Static voidMain (string[] args)7         {8             if(args==NULL)9             {Ten                 Throw NewArgumentNullException (nameof (args)); One             } A         } -     } -}

We replace the fixed "args" with the equivalent nameof (args) . By convention, the Il of the code is posted in two ways.

Il code for "args" mode:

1 . Method Private HidebysigStaticvoidMain (string[] args)CIL managed2 {3   . EntryPoint4   //Code Size (0x16)5   . Maxstack  26   . Locals Init([0]BOOLv_0)7   il_0000:  NOP8   il_0001:  ldarg.09   il_0002:  ldnullTen   il_0003:ceq One   il_0005:  stloc.0 A   il_0006:  ldloc.0 -   il_0007:  BRFALSE.Sil_0015 -   il_0009:  NOP the   il_000a:  ldstr      "args" -   il_000f:  newobj     instance void[mscorlib] System.argumentnullexception::.ctor (string) -   il_0014:  Throw -   il_0015:  ret +}//end of Method Program::main

Il code for the nameof (args) method:

1 . Method Private HidebysigStaticvoidMain (string[] args)CIL managed2 {3   . EntryPoint4   //Code Size (0x16)5   . Maxstack  26   . Locals Init([0]BOOLv_0)7   il_0000:  NOP8   il_0001:  ldarg.09   il_0002:  ldnullTen   il_0003:ceq One   il_0005:  stloc.0 A   il_0006:  ldloc.0 -   il_0007:  BRFALSE.Sil_0015 -   il_0009:  NOP the   il_000a:  ldstr      "args" -   il_000f:  newobj     instance void[mscorlib] System.argumentnullexception::.ctor (string) -   il_0014:  Throw -   il_0015:  ret +}//end of Method Program::main

The same, I did not see any differences,, so, this operator is also a compiler level provided by the syntax of sugar, after compiling there is no nameof shadow.

2. Nameof Precautions

Nameof can be used to get a simple string representation of the current name of a named expression (not a fully qualified name). Notice the current name of the qualification, such as the following example, what do you think will be output results?

1 using static system.console;2 using CC = System.consolecolor;3 4 namespace Csharp65 {6Internalclass Program7     {8         PrivateStaticvoidMain ()9         {TenWriteLine (nameof (CC));//CC OneWriteLine (nameof (System.consolecolor));//Consolecolor A         } -     } -}

The first statement outputs "CC" because it is the current name, although it is an alias to the System.consolecolor enumeration, but since CC is the current name, the result of the nameof operator is "CC".

The second statement outputs "Consolecolor" because it is a simple string representation of System.consolecolor instead of obtaining its fully qualified name, and if you want to get "System.consolecolor", use typeof ( System.consolecolor). FullName . Another example is Microsoft's case: nameof (person). Address.zipcode) , the result is "ZipCode".

3. Reference

C # Language Reference-Operator: nameof

[C#6] 2-nameof operator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.