BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 3.0+

Setting Automatic Property Values in a Struct's Constructor

When initialising the properties of a structure using a custom constructor, you can write to the backing variables to set the property values indirectly. When using automatically implemented properties, these backing variables are unavailable.

Setting Properties in a Struct Constructor

When creating a structure using the .NET framework 2.0 or earlier, or when using fully described properties with backing variables in .NET 3.0 or later, you can initialise properties in a constructor by modifying the backing variables directly. When using C# 3.0's automatically implemented property syntax, the backing variables are never explicitly defined. To set a property's value, you must assign to the property itself.

An example of a structure that uses the full syntax for implementing a property is as follows. You can see that the constructor sets the dimensions of the Square structure during instantiation.

public struct Square
{
    private int _dimensions;

    public Square(int dimensions)
    {
        _dimensions = dimensions;
    }

    public int Dimensions
    {
        get { return _dimensions; }
        set { _dimensions = value; }
    }

    public int Area { get { return Dimensions * Dimensions; } }
}

One of the problems that is encountered by C# developers when using structures and automatically implemented properties is due to a limitation when accessing the "this" object. Until all of the fields of a structure are defined, you may not use the "this" object. This means that property values cannot be set in a constructor immediately. We can demonstrate this by modifying the above example to use automatically implemented property syntax:

public struct Square
{
    public Square(int dimensions)
    {
        Dimensions = dimensions;
    }

    public int Dimensions { get; set; }

    public int Area { get { return Dimensions * Dimensions; } }
}

If you attempt to compile this structure the error, "The 'this' object cannot be used before all of its fields are assigned to" is displayed and the build fails. The problem is that the hidden backing store field is not initialised and so the property cannot be accessed.

To resolve this issue, you can simply call the default constructor of the structure. Unlike with classes, a struct always includes a default constructor that accepts no parameters. The constructor initialises all of the fields of the structure to their defaults values. Once initialised, your custom constructor can reset them to the desired values.

To call the default constructor, modify the custom constructor declaration as follows:

public Square(int dimensions): this()

This syntax can also be used for standard properties where you wish to initialise a property by calling its set accessor, rather than by assigning a value to its backing variable.

15 March 2009