Look at what the syntactic sugar in C #6.0 has done (final ),

Source: Internet
Author: User

Look at what the syntactic sugar in C #6.0 has done (final ),

Finally, I wrote the final article. The whole article is like a sleepwalking. After I finish this article, I have to continue writing my js series.

 

I. Object initializer with indexes

Follow the old rules of the rivers and lakes to see what it is.

1         static void Main(string[] args)2         {3             Dictionary<string, string> dic = new Dictionary<string, string>()4             {5                 ["Name"] = "ctrip",6                 ["Age"] = "15"7             };8         }

At first glance, we can see that this is quite fresh, but in the twinkling of an eye we can see whether it is similar to the array initiator and the object initiator? If you think this is the right way, let's take a look at this stuff to generate

What kind of IL.

 

 

We can clearly see that the set_Item method is the sugar in the index operator on the top of the compiler, which means we can improve the development efficiency, but it is also okay, at least let me enter less

And then the code is restored as follows:

1             Dictionary<string, string> dic = new Dictionary<string, string>();2             dic["Name"] = "ctrip";3             dic["Age"] = "20";

 

The internal code of this indexer method can be seen as an Insert operation.

2. struct constructors without Parameters

I don't know how many people know that the default constructor cannot be defined before C #6.0. Why? The principle is simple, because the value type and reference type have different mechanisms,

The value type can allocate space in the stack without the need for new. For example, if we define the struct Point below, we can easily use the point. X value.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 using System. console; 7 8 namespace ConsoleApplication3 9 {10 class Program11 {12 static void Main (string [] args) 13 {14 Test t = new Test (); 15 var s = t. point. x; // is there any call to the constructor? Ah, ah, ah ............... 16 Console. read (); 17} 18} 19 20 public class Test21 {22 public Point point; 23} 24 25 public struct Point26 {27 public int X; 28 29 public int Y; 30 31 public Point () 32 {33 X = 5; 34 Y = 5; 35} 36} 37}

If the default constructor is executed, I will output 5 in point. X. Is it a bit strange? Therefore, for this reason, the default value C #6.0 is disabled to avoid suspicion.

Type constructor. But this time C #6.0 was released, so I can't wait to see whether the default constructor is called, for example:

 

 

The default constructor has not been called. Now I know that it is called only when I am new, so I find that, the value type is used to simulate the reference type,

I personally feel that it is really unnecessary to open this restriction.

 

 

Iii. Exception Filter

In C #6.0, this exception filter is really a novelty. If you don't know it, you can just take a look at it, for example, the following code.

 1         public void Run() 2         { 3             try 4             { 5             } 6             catch (Exception ex) 7             if (ex.Message.Contains("timeout")) 8             { 9                 throw;10             }11         }

 

If you take a closer look, it seems that {} is omitted in a catch? I didn't see any other special things, and then I was very curious to restore the above Code to version 6.0,

The Code is as follows:

 1         public void Run1() 2         { 3             try 4             { 5  6             } 7             catch (Exception ex) 8             { 9                 if (ex.Message.Contains("timeout"))10                 {11                     throw;12                 }13             }14         }

Next, let's see what the IL of the two codes looks like? The heart is cool, ah .... They're all paralyzed .....

 

As you can see, the above two pieces of code seem to be the same, in fact, the generated IL is still very different, the new version of code will use isinst to determine whether it is an Exception instance, and brtrue is used to determine whether or not the current

Is null. If it is null, the Statement ex. Message. Contains ("timeout") will not be executed. However, the old version of the Code does not determine whether it is true or false. The code is still executed normally, so now you can know that,

In fact, it is not simple to omit the braces "{}". This syntactic sugar still has some intelligent judgment at the bottom layer.

 

Now, all the syntactic sugar analysis of c #6.0 is over. Thank you for your attention.

 

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.