Initialization with => (such that)

I studied the source code of Entity Framework 7 on github , and I found the following property initialization in TableExpressionBase.cs

public override ExpressionType NodeType => ExpressionType.Extension; 

I have never seen such use of the => operator in C #. I also looked at what's new in C # 6.0, however I did not find this construct. Can anyone explain what his purpose is?

Thanks.

+6
source share
2 answers

This is the new expression syntax that has been added in C # 6.0.

This article has a good summary of the things that have been added, find the heading "Expression Bodied Functions and Properties" about 3/4 down the article.

In C # 6.0, a lot of syntax has been added that generates code under the hood. This does not allow you to do things that you could not do before, but it makes the number of lines of code that you should write less.

In particular, if you have a property like this:

 public TYPE Name { get { return EXPRESSION; } } 

Then you can write this property as follows:

 public TYPE Name => EXPRESSION; 

The compiled code will be identical, so you can choose which of the two syntax options you want to use.

You can do the same with the methods:

 public string Name(int PARAM1, string PARAM2) { return string.Format("{0}, {1}", PARAM1, PARAM2); } 

can be:

 public string Name(int PARAM1, string PARAM2) => string.Format("{0}, {1}", PARAM1, PARAM2); 

That is all that is needed.

In particular, the property that you saw in the EF7 code is basically the same:

 public override ExpressionType NodeType { get { return ExpressionType.Extension; } } 
+2
source

This is a new feature in C # 6.0, as you suspected.

In properties and indexers, only recipients and setters can be; properties and indexers can have an expression body:

 public string Name => First + " " + Last; public Customer this[long id] => store.LookupCustomer(id); 

http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx

Its just a shorter way to define a getter for a property.

+1
source

Source: https://habr.com/ru/post/989446/


All Articles