#1,220 – C# 6.0 – Defining a Parameterless Constructor for a struct

In C# 5.0, every struct had a default parameterless constructor that you couldn’t override.  Using the new operator invoked this constructor and all members of the struct were assigned default values.

    public struct MyPoint
    {
        public double X;
        public double Y;
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyPoint pt = new MyPoint();      // default values for X, Y (0.0)
            Console.WriteLine("{0}, {1}", pt.X, pt.Y);
        }
    }

1220-002

In C# 6.0, you can explicitly define a parameterless constructor for a struct, giving non-default values to the members of the struct.

        public struct MyPoint
        {
            public double X;
            public double Y;

            public MyPoint()
            {
                X = 100.0;
                Y = 100.0;
            }
        }

        static void Main(string[] args)
        {
            MyPoint pt = new MyPoint();      // 100.0, 100.0
            Console.WriteLine("{0}, {1}", pt.X, pt.Y);
        }

1220-001

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

15 Responses to #1,220 – C# 6.0 – Defining a Parameterless Constructor for a struct

  1. Glad you are back! I am enjoying your posts!

  2. Marcel says:

    Hi Sean,

    Glad to see you’re back.

    Marcel.

  3. Pingback: Dew Drop – April 2, 2015 (#1986) | Morning Dew

  4. Anantha cholan says:

    Hi I had a doubt. why c# 4.0 and lower doesn’t support parameter less constructor? Thanks in advance.

  5. Kevin says:

    Very welcome back!!

  6. Mark V says:

    I’ve always wondered why structs couldn’t inherit from other structs. There wouldn’t need to be any polymorphism, if that’s too hard – just sharing of fields and methods, to avoid copy/pasting the code in similar structs. Yes, you can say “use classes instead of structs” but then you have the null problem; using a struct makes be a value type.a

  7. illidans4 says:

    Quite a controversial change.

  8. Steven says:

    Took me 1 and a half weeks to go thru all of your posts. Great stuff. Looking forward to your next posts. Thanks for your time!

  9. Agent Bond says:

    This feature has been rolled back. So, the C# version attributed to VS 2015 RC doesn’t support parameterless constructor for structures.

  10. codest says:

    First of all, thanks for your efforts of these great c# and WPF sites

    1) Is this c# site has been abandoned? It has not update since December 2015.
    2) There are some c# 6.0 features post, but had been drawback in final release, which make them misleading. Can you update these posts to clarify them?

  11. siddharth agarwal says:

    I think We cannot define Parameterless Constructor for a struct

    • Sean says:

      That’s correct. This post was written about a pre-release version of C# 6 that did include them. They ultimately were dropped as a feature in the final release.

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: