3

I have the following code :

struct Person
    {
        public readonly int x;

        public Person( int x )
        {
            this.x = x;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            Console.Write(p.x);
        } 
    }

This code work well.Why? Did overriding default constructor not be applied to structs ? Did using a parameterized constructor override the default one or not ?

Brahim Boulkriat
  • 984
  • 2
  • 9
  • 21

4 Answers4

7

You didn't override the default constructor; you just provided an overload which accepts one parameter. Unlike classes, having a parameterized constructor for a struct doesn't mean that the default constructor won't be automatically generated. The C# compiler automatically provides a default, parameterless constructor for structs, and it does not allow you to override it with your own.That's simply the nature of structs.

From Using Structs (C# Programming Guide)

It is an error to define a default (parameterless) constructor for a struct. It is also an error to initialize an instance field in a struct body. You can initialize struct members only by using a parameterized constructor or by accessing the members individually after the struct is declared. Any private or otherwise inaccessible members can be initialized only in a constructor.

If you really want to require users of your data type to call a custom constructor, you'll have to use a class instead.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • 1
    Actually, the zero-parameter constructor "public Person() { ... }" is not really there. The C# compiler does not generate it! The pitfall is the C# compiler always treats `new X()` as the same as `default(X)` for all value types. So the value of `X` where all instance fields are zero/null/etc. is used. No constructor code is actually run. – Jeppe Stig Nielsen Nov 01 '16 at 09:16
1

http://msdn.microsoft.com/en-us/library/aa288208%28v=vs.71%29.aspx

Structs cannot contain explicit parameterless constructors. Struct members are automatically initialized to their default values

Some good explanation Why can't I define a default constructor for a struct in .NET?

Community
  • 1
  • 1
sh1ng
  • 2,808
  • 4
  • 24
  • 38
1

Structs will always have a default parameterless constructor which you cannot overrride. MSDN explains a little. This (and other) limitations are there because structs are value types. If you need to use them like objects, use objects :)

juhan_h
  • 3,965
  • 4
  • 29
  • 35
0

In your code, you are not calling the constructor you created, but rather the default constructor.

To call the constructor you overrode, try the following code:

struct Person
{
    public readonly int x;

    public Person( int x )
    {
        this.x = x;
    }
}

class Program
{
    static void Main(string[] args)
    {
        int v = 9;

        Person p = new Person(v);
        Console.Write(p.x); // will output '9'
    } 
}
rhughes
  • 9,257
  • 11
  • 59
  • 87