locked
Structure constructors

    Question

  • Why does c# have these wierd rules that every structure construtor must have atleast 1 parameter and that the constructor must initialize each one of the fields, and also that fields can't be initialized to a default value.

    Due to these rules I can't do the following

    - Have a default constructor(which sets each value to 0), since the constructor can't be parameterless.
    - Have a default value of a field and not repeatedly initialize it to the same value in every one of the constructors, since fields cant be initialized to a default value.
    - Initialize some of the fields at runtime to preserve memory, since the constructors insist on initializing them.

    Any logic behind these strange rules?
    Monday, June 16, 2008 8:59 PM

Answers

  • Because structures are simply an inline area of memory, they cannot be null, and so the CLR has to be able to ensure that the area of memory is totally initialized rather than being partly garbage. For this reason, you'll often hear the 'constructors' on structures called (arguably more correctly) 'initializers' because the don't construct an object they just initialize an area of memory.

    You can't define a default initializer because the CLR has an intrinsic understanding of structures and automatically zeroes out the required amount of memory for a default structure.

    When you define a non-default initializer, C# requires you to set all fields because it skips the zeroing of memory and lets you initialize it - otherwise you'd have to have a double initialization performance hit. If you don't care about the (very slight) performance hit you can always chain a call to the : this() initializer and then only initialize selected fields.

    So yes, there is logic to the strange rules. And they're not so strange when you understand why they exist!

    http://gregbeech.com/blogs/tech
    • Proposed as answer by Greg Beech Tuesday, June 17, 2008 11:10 PM
    • Marked as answer by Mudit Jain Tuesday, June 17, 2008 11:50 PM
    Monday, June 16, 2008 9:49 PM

All replies

  • Because structures are simply an inline area of memory, they cannot be null, and so the CLR has to be able to ensure that the area of memory is totally initialized rather than being partly garbage. For this reason, you'll often hear the 'constructors' on structures called (arguably more correctly) 'initializers' because the don't construct an object they just initialize an area of memory.

    You can't define a default initializer because the CLR has an intrinsic understanding of structures and automatically zeroes out the required amount of memory for a default structure.

    When you define a non-default initializer, C# requires you to set all fields because it skips the zeroing of memory and lets you initialize it - otherwise you'd have to have a double initialization performance hit. If you don't care about the (very slight) performance hit you can always chain a call to the : this() initializer and then only initialize selected fields.

    So yes, there is logic to the strange rules. And they're not so strange when you understand why they exist!

    http://gregbeech.com/blogs/tech
    • Proposed as answer by Greg Beech Tuesday, June 17, 2008 11:10 PM
    • Marked as answer by Mudit Jain Tuesday, June 17, 2008 11:50 PM
    Monday, June 16, 2008 9:49 PM
  • It all has to do with optimization.
     
    You can't specify a default constructor because it is provided by the runtime.  When a struct is created with the default constructor, the runtime simply zeros out all the fields in the struct.  This allows it to initialize large arrays of structs relatively quickly by zeroing out the entire block of memory.  Otherwise, the runtime would have to call the constructor for each element in the array of structs.  Note that, this isn't true for reference types, since an array of reference types is just initialized to null.
    Monday, June 16, 2008 9:51 PM
  •  

    Value type (struct) constructors work quite differently from reference type (class) constructors.

    The common language runtime always allows the creation of value type instances,

    and there is no way to prevent a value type from being instantiated. For this reason, value

    types don't actually even need to have a constructor defined within them. For this reason,

    many compilers (including C#) don't emit default parameterless constructors for value types.


    Haider Raza Software Engineer, ZABSolutions.
    Tuesday, June 17, 2008 9:26 AM