What is the difference between getter-only auto properties and expression body properties?

They are syntactic sugar for two different things. The former initializes a backing field, and sets it to the expression on the right hand side of the assignment during field initialization. The latter creates a get that does exactly what is in the expression.

public override bool CanRead { get; } = true;

is equivalent to

private readonly bool __backingFieldCanRead = true;

public override bool CanRead
{
    get
    {
        return __backingFieldCanRead;
    }
}

This

public override bool CanRead => true;

is equivalent to

public override bool CanRead
{
    get
    {
        return true;
    }
}

They behave differently. The first case sets the value of the property when the object is created and the field are initialized, the other case evaluates the expression every time the property’s getter is invoked. In the simple case of a bool, the behavior is the same. However if the expression causes side effects, things are different. Consider this example:

class Program
{
    static void Main(string[] args)
    {
        var fooBar1 = new FooBar();
        Console.WriteLine(fooBar1.Baz);
        Console.WriteLine(fooBar1.Baz);
        var fooBar2 = new FooBar();
        Console.WriteLine(fooBar2.Baz);
        Console.WriteLine(fooBar2.Baz);
    }
}

public class FooBar
{
    private static int counter;
    public int Baz => counter++;
}

Here, “0, 1, 2, 3” are printed. The static counter field is incremented every time the property’s getter is invoked. However, with a property initializer:

public int Baz { get; } = counter++;

Then “0, 0, 1, 1” is printed because the expression is evaluated in the object’s constructor.

Leave a Comment