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 ),

Today, it's okay. I just got a preview of vs2015. Some time ago, the garden was also excited with these new syntactic sugar. Here we will look at what kind of IL will be generated?

 

I. attributes are automatically initialized.

It is indeed simpler than the previous version, but you must be curious about what the compiler has done for us?

1     class Student 2  { 3 public string Name { get; set; } = "ctrip"; 4 }

 

From this figure, we can see that in ctor, the value of <Name> k _ backingfield = "ctrip" is assigned before base: ctor, which indicates that name is a variable initialization value, does not belong

What is the difference between the assignment of constructor values? If base: ctor is before <Name> k _ backingfield = "ctrip", it is the assignment of constructor values, but I have to specify

Here is the difference at the source code level, rather than the difference in IL, because in IL, all constructors assign values, but the order of statements is different. Then I will restore the Internal Code as follows:

 1     class Student  2  {  3 private string k__BackingField = "ctrip";  4  5 public string Name  6  {  7 get  8  {  9 return k__BackingField; 10  } 11 12 set 13  { 14 k__BackingField = value; 15  } 16  } 17 }

Then let's see how to make base: ctor before <Name> k _ backingfield = "ctrip.

 1     class Student  2  {  3 private string k__BackingField;  4  5 public string Name  6  {  7 get  8  {  9 return k__BackingField; 10  } 11 12 set 13  { 14 k__BackingField = value; 15  } 16  } 17 18 public Student() 19  { 20 k__BackingField = "ctrip"; 21  } 22 }

 

Sorry, the differences between variable initialization and constructor assignment at the source code level were accidentally pulled.

 

Ii. Read-only attribute Initialization

This is also a super fun property. Let's take a look at the Code:

1     class Student 2  { 3 public string Name { get; } 4 5 public Student(string name) 6  { 7 Name = name; 8  } 9 }

However, we remember that the previous C # version cannot be written in this way, but it cannot be used anymore. Let's go to the underlying layer to see what has been generated.

 

 

Then I was surprised that the attribute could have been read-only. Now that the compiler has been released, is there a problem? If I really need a read-only attribute, how should I do it?

What about it? Then I tried to return a value in the Name attribute, and the compiler did not allow it. This shows that the compiler has made a reasonable judgment in it.

 

Iii. Lambda acts as the function body

This sounds a little strange. Let's take a look at the example first.

1     class Student 2  { 3 public string Name => "ctrip"; 4 5 public void Print(string name) => Console.WriteLine(name); 6 }

However, when I see this writing method, I am also drunk. If you haven't touched C # for a year, and then come back, I think you will certainly not understand these chicken codes... No way. You have to continue.

What does IL do at the underlying layer?

 

When we see IL, we get drunk again. In fact, => is just a {} method bracket! Doesn't this increase our learning costs? Then I will continue to think that the function body here is

Console. wirteline statements, what if I add a few statements? As you can imagine, it must be parentheses, but after I add {}, the compiler is messy...

 

The figure tells us that the lamaba of C #6.0 serves as the syntax sugar of the function body only for one statement. If multiple statements are required, you can extract only one method separately,

As shown below.

 

Well, this is the general case in the previous article. It's not too early to withdraw it first.

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

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.