See what those grammar candies in C # 6.0 do (medium)

Source: Internet
Author: User

Then the last article continues to pull, in fact, the grammar of sugar is not a bad thing, the first is to eat not eat with you, the second is best to know what these sugar in the bottom have done something, but a little

Call to see for the real, so as to ease the use of the five floor, not laborious.

One: String embedding value

I think the String.Format method is to be turned into ashes that everyone knows, such as the following code:

1     classBird2     {3         Private stringName ="Swallow";4 5          Public voidFly ()6         {7             varresult =string. Format ("Hello {0}", Name);8         }9}

A bad thing about this format is that if there are too many placeholders, it's especially easy to make a mistake, and if you lose a parameter, the code will get an error.

Next, take a ride and look at string. The format underlying code, or quite surprised to find, in fact, but the bottom of the call is the Stirngbuilder.appendformat method.

Because it is easy to error, so in order to insure the use of string concatenation to achieve, but I also know that string concatenation is a time-consuming operation, write a StringBuilder and too troublesome,

Fortunately c#6.0 provides a fresh way to play, first look at the code:

1     classBird2     {3         Private stringName ="Swallow";4 5          Public voidFly ()6         {7             //var result = string. Format ("Hello {0}{1}", Name);8 9             varresult ="\{"Hello"}:\{name}";Ten  One Console.WriteLine (result); A         } -}

And then can't wait to see how the bottom of the game, in fact, in the following IL diagram can be seen, in the bottom end still called the String.Format method to achieve.

Two: Using static class

This kind of writing looks a bit nondescript, at first glance is not useful, but can tell us a principle, that no matter how you change the upper layer, the compiler is still used

All named, this is called original Aim bar.

Three: null value judgment

Let's take a look at the tolerance distortion of this play.

1     class Bird 2     {3public         void Fly (string  name)4          {5             var result = name?. Length; 6         }7     }

Does it look a little dizzying? That's right, the compiler is such a quiet end of the bowl looking at us to write these loaded code, but how to install force, but also escaped ildasm eyes.


Actually look after the IL code carefully, feel all is still so familiar, the emphasis is this brtrue.s. Its state also determines the two execution flow, but on Il also saw v_1 this compilation

The device gives us a separate variable defined by the code, which is restored as follows:

1     classBird2     {3          Public voidFly (stringname)4         {5             int?R;6 7             if(Name = =NULL)8             {9                 int? V_1 =Newnullable<int>();Ten  OneR =v_1; A             } -             Else -             { theR =Newnullable<int>(name. Length); -             } -         } -}

Four: nameof expression

When I know the purpose of this keyword, my first reaction is the Logmanager class in the company framework, and when we new Logmanager, we will also put the current class name

Pass on, then do some post-processing, but in the past we can only do so, either with reflection, or write dead.

1 namespaceConsoleApplication32 {3     class Program4     {5         Static voidMain (string[] args)6         {7             //First: Use reflection8             varIlog =NewLoggermanager (typeof(program));9 Ten             //The second kind: Write Dead OneIlog =NewLoggermanager (" Program"); A  -Console.WriteLine (" World"); -         } the     } -  -     classLoggermanager -     { +         /// <summary> -         ///the constructor records the class name +         /// </summary> A         /// <param name= "type" ></param> at          PublicLoggermanager (Type type) -         { -             //Todo - Console.WriteLine (type. Name); -         } -          PublicLoggermanager (stringclassName) in         { -             //Todo to Console.WriteLine (className); +         } -     } the}

I think we can see that the first use of reflection, which is the need to read metadata, performance you understand, the second although the string, you see, is the way to write dead, this time

An enhanced version is urgently needed, as follows.

After seeing IL, I was moved by the chicken anyway ... Nameof has the advantages of both the above, flexible, high performance .... Good good, praise a bit.

See what those syntactic sugars in C # 6.0 do (medium)

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.