Automatically implemented property in struct can not be assigned

From the C# Specification:

10.7.3 Automatically implemented properties

When a property is specified as an automatically implemented property,
a hidden backing field is automatically available for the property,
and the accessors are implemented to read from and write to that
backing field.

[Deleted]

Because the backing field is inaccessible, it can be read and written
only through the property accessors, even within the containing type.

[Deleted]

This restriction also means that definite assignment of struct types
with auto-implemented properties can only be achieved using the
standard constructor of the struct, since assigning to the property
itself requires the struct to be definitely assigned. This means that
user-defined constructors must call the default constructor.

So you need this:

struct T 
{
    public T(int u)
        : this()
    { 
        this.U = u;
    }

    public int U { get; private set; }
}

Leave a Comment