#298 – A struct Can Have Several Constructors

You can define more than one constructor for a struct, as long as the method signature for each constructor is different.

    public struct Point3D
    {
        public double x;
        public double y;
        public double z;

        public Point3D(double xi, double yi, double zi)
        {
            x = xi;
            y = yi;
            z = zi;
        }

        public Point3D(int n)
        {
            x = n;
            y = n;
            z = n;
        }
    }

Now you can instantiate a variable of this type in several different ways.

            Point3D pt1 = new Point3D();    // Default parameterless constructor
            Point3D pt2 = new Point3D(10.0, 20.0, 30.0);
            Point3D pt3 = new Point3D(5);
Advertisement

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

3 Responses to #298 – A struct Can Have Several Constructors

  1. Dear Sean!

    I was pleased
    In C # is not possible to define a Konstrukor 😦
    I did not succeed

    Visual Studio 2013

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 )

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: