#645 – You Can Chain to the Default Constructor for a struct

You can use the this keyword in a constructor within a struct to invoke other constructors (constructor chaining).

public BoxSize(double x, double y)
    : this(x, y, 1.0)
    {
    }

Every struct also has a default parameterless constructor that you can explicitly invoke when creating an object of the struct’s type.

            BoxSize bs = new BoxSize();

Additionally, you can use the this keyword to chain to this default constructor from another constructor.  Doing so results in all fields being assigned to default values.

        // Initialize x and y to specified values,
        //   initialize z to default value.
        public BoxSize(double x, double y)
            : this()
        {
            this.x = x;
            this.y = y;
        }
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: