Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialization with => (such that)

I've been looking into Entity Framework 7 source code on github and I've found following property initialization in TableExpressionBase.cs

public override ExpressionType NodeType => ExpressionType.Extension;

I have never seen such usage of => operator in C#. I've also looked what is new in C# 6.0, however I haven't found this construct. Can someone explain what is the purpose of it?

Thanks.

like image 505
interjaz Avatar asked Jun 20 '15 16:06

interjaz


People also ask

What is initialization give example?

Initialization is the process of locating and using the defined values for variable data that is used by a computer program. For example, an operating system or application program is installed with default or user-specified values that determine certain aspects of how the system or program is to function.

What is initialization in C++ with example?

[edit] Initialization of a variable provides its initial value at the time of construction. The initial value may be provided in the initializer section of a declarator or a new expression. It also takes place during function calls: function parameters and the function return values are also initialized.

What do you mean by initialization?

initialized; initializing. transitive verb. : to set (something, such as a computer program counter) to a starting position, value, or configuration.

How do you initialize an object?

Objects can be initialized using new Object() , Object. create() , or using the literal notation (initializer notation). An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ( {} ).


1 Answers

This is the new expression-bodied members syntax that was added in C# 6.0.

This article has a good rundown of the things that was added, look for the heading "Expression Bodied Functions and Properties" about 3/4 down the article.

In C# 6.0 a lot of syntax was added that generates code under the hood. It doesn't allow you to do stuff that you couldn't do before, but it makes the number of lines of code you have to write smaller.

Specifically, if you have a property like this:

public TYPE Name
{
    get
    {
        return EXPRESSION;
    }
}

Then you can now write this property like this:

public TYPE Name => EXPRESSION;

The compiled code will be identical so you can pick and choose which one of the two syntax variations you want to use.

You can do the same thing with methods:

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

can become:

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

That's all there is to it.

Specifically, the property you saw in the EF7 code is basically the same as this:

public override ExpressionType NodeType
{
    get
    {
        return ExpressionType.Extension;
    }
}
like image 54
Lasse V. Karlsen Avatar answered Oct 13 '22 14:10

Lasse V. Karlsen