Arrow Properties in C#

C# allows short property implementations to use the arrow token (=>). This syntax is called an expression-bodied member because the member consists of a single expression whose result is returned or assigned automatically.

When was it introduced?

Expression-bodied members were introduced in C# 6. Read-only expression-bodied properties were part of that release. C# 7 expanded the feature so that individual property accessors could also use expression bodies.

The syntax is useful when a property has a simple calculation or directly returns another value. It is not a replacement for a property that needs multiple statements or more involved logic.

Read-only properties

Without the arrow notation, a calculated read-only property uses a regular get accessor:

public class Rectangle
{
    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public double Width { get; }
    public double Height { get; }

    public double Area
    {
        get { return Width * Height; }
    }
}

With the arrow notation, the same property is shorter and expresses its intent directly:

public class Rectangle
{
    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public double Width { get; }
    public double Height { get; }

    public double Area => Width * Height;
}

The expression after => is evaluated whenever Area is read. It is not stored as a separate value.

Expression-bodied accessors

C# 7 and later also allow individual accessors to use arrow notation. This is useful when a property has both a getter and a setter with simple logic.

Without arrow-bodied accessors:

public class Temperature
{
    private double _celsius;

    public double Fahrenheit
    {
        get { return _celsius * 9 / 5 + 32; }
        set { _celsius = (value - 32) * 5 / 9; }
    }
}

With arrow-bodied accessors:

public class Temperature
{
    private double _celsius;

    public double Fahrenheit
    {
        get => _celsius * 9 / 5 + 32;
        set => _celsius = (value - 32) * 5 / 9;
    }
}