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

Source: Internet
Author: User

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

Reference page:

Http://www.yuanjiaocheng.net/CSharp/csharp-generic-sortlist.html

Http://www.yuanjiaocheng.net/CSharp/csharp-generic-dictionary.html

Http://www.yuanjiaocheng.net/CSharp/csharp-partial-class.html

Http://www.yuanjiaocheng.net/CSharp/csharp-Static.html

Http://www.yuanjiaocheng.net/CSharp/csharp-Anonymous-method.html

 

Next I will continue to talk about it in the previous article. In fact, syntactic sugar is not a bad thing. The first is to eat or not, and the second is to know what the sugar has done at the bottom.

In this way, you can use it with peace of mind. It is not difficult to go to the fifth floor in one breath.

 

I. String embedding Value

I think the String. Format method is converted to Gray. Everyone knows it, for example, the following code:

1     class Bird2     {3         private string Name = "swallow";4 5         public void Fly()6         {7             var result = string.Format("hello {0}", Name);8         }9     }

One bad thing about this Format is that if there are too many placeholders, it is very easy to make a mistake. If you miss a parameter, the code will report an error.

 

Next, let's take a look at the underlying code of string. Format. We are surprised to find that the underlying code is actually called the StirngBuilder. AppendFormat method.

 

It is easy to report errors, so we use String concatenation for the sake of insurance. However, I also know that String concatenation is a time-consuming operation, and it is too troublesome to write a StringBuilder,

Fortunately, C #6.0 provides a new method. Let's look at the code first:

 1     class Bird 2     { 3         private string Name = "swallow"; 4  5         public void Fly() 6         { 7             //var result = string.Format("hello {0}{1}", Name); 8  9             var result = "\{"hello"}:\{Name}";10 11             Console.WriteLine(result);12         }13     }

 

Then I can't wait to see how to play at the underlying layer. In fact, we can see in the IL diagram below that the String. Format method is finally called at the underlying layer for implementation.

 

 

Ii. using static class

This kind of writing seems a little nondescribable, And it is useless at first glance, but it can tell us a principle that no matter how you change the upper layer, the compiler will still use it the same way.

Full name. This is the case of changing things.

 

Iii. null value judgment

Let's take a look at the content of this method.

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

Does it look a little dizzy? That's right. The compiler is so quietly watching us write these forced code, but how to install it can not escape the eyes of ILdasm.


 

In fact, after carefully reading the IL code, I feel that everything is still so familiar, with the focus on brtrue. s. Its status also determines the two execution streams, but we also see the V_1 compilation on the IL.

The code is restored as follows:

 1     class Bird 2     { 3         public void Fly(string name) 4         { 5             int? r; 6  7             if (name == null) 8             { 9                 int? V_1 = new Nullable<int>();10 11                 r = V_1;12             }13             else14             {15                 r = new Nullable<int>(name.Length);16             }17         }18     }

 

Iv. nameof expression

When I know the purpose of this keyword, my first response is the LogManager class in the company framework. When we use new LogManager, the current class name is

Pass on, and then do some post-processing, but in the past we had to do this, either using reflection or writing to death.

1 namespace ConsoleApplication3 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // first type: use reflection 8 var ilog = new LoggerManager (typeof (Program); 9 10 // type 2: write dead 11 ilog = new LoggerManager ("Program"); 12 13 Console. writeLine ("world "); 14} 15} 16 17 class LoggerManager18 {19 // <summary> 20 // The constructor records the following class name 21 /// </summary> 22 // <param name = "type"> </param> 23 public LoggerManager (Type type) 24 {25 // todo26 Console. writeLine (type. name); 27} 28 public LoggerManager (string className) 29 {30 // todo31 Console. writeLine (className); 32} 33} 34}

 

I think you can also see that the first method uses reflection, which requires reading metadata. You know the performance, and the second method, although it is a string, you can also see that it is a dead-end method, at this time

An enhanced version is urgently needed, just like below.

After seeing IL, I am a chicken... Nameof has the advantages of the above two, both flexible, high performance .... Nice, like it.

 

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.