Lambda for getter and setter of property

First of all, that is not lambda, although syntax is similar.

It is called “expression-bodied members“. They are similar to lambdas, but still fundamentally different. Obviously they can’t capture local variables like lambdas do. Also, unlike lambdas, they are accessible via their name:) You will probably understand this better if you try to pass an expression-bodied property as a delegate.

There is no such syntax for setters in C# 6.0, but C# 7.0 introduces it.

private int _x;
public int X
{
    get => _x;
    set => _x = value;
}

Leave a Comment