#643 – The Constructor for a struct Must Initialize All Data Members

.NET requires that you initialize all memory for an object before you use it.  When it comes to a struct, this means that you must do one of three things when declaring a new instance of a struct:

  • Invoke the default parameterless constructor, which initializes all elements within the struct.  (E.g. zeroes out simple value types)
  • Declare the new instance and then set data members explicitly (assuming that they are all public)
  • Invoke a custom constructor, which initializes all data members

This means that if you define a non-default constructor for a struct, your constructor must initialize all data members in the struct before it returns. to the caller.  This guarantees that any creation of a struct-based value type will result in an object which is safe to use, because all of its data members have known values.

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: