7

Is there any way to prevent the default constructor of a struct to be called?

I have several structs in my project and some of them I can not let the default constructor to be called at any circumstances (that will be cause a lot of unwanted behaviour in my code).

PS.: Any solution that just indicates me when coding that some of that special structs (that can not be instantiated with the default constructor) are being instantiate "wrongly" is fine for me. A simple compiler warning should be enough for my case!

3
  • Making the constructor private should suffice?!
    – ElDuderino
    Apr 13, 2016 at 16:47
  • 1
    Default struct constructors in C# aren't constructors at the CLR level. They are strictly zero initialization constructs made to look (syntactically) like constructors. What kind of unwanted behavior do they cause? Can you tell us more about your use case? Apr 13, 2016 at 16:48
  • I will change my structs to classes guys. I was using structs because I was trying to set blittable custom types, but in my specific scenarious it seems to be impossible. Apr 13, 2016 at 17:17

3 Answers 3

10

You cannot.

By nature,

  • all fields of a struct must be initialized to some value.
  • a struct cannot have a parameter-less constructor
  • the parameter-less constructor will be implicitly called, there's no way around it.

Either use a class if you need such behavior or review your design.

2
  • I will change the type for a class. Making a class's default constructor private I ensure it will not be called at ANY circumstances? Thanks! Apr 13, 2016 at 17:15
  • @andresantacruz Even if you make a struct's constructor private, values will still be created from a range of zero-byte memory - consider what happens with this: struct Foo { public Int32 X; } void() { Foo[] structArray = new Foo[100]; structArray[50].X = 123; }.
    – Dai
    May 8, 2020 at 23:14
5

When you're dealing with structs, C# wants to make sure that you've initialized all of the struct's fields.

One option is to provide a constructor that will call the default constructor first, then do your custom initialization later:

 public S(int x) : this()
 {
     /* custom initialization logic */
 }

Another option is to provide a constructor that provably initializes all fields of the struct in its own body, in which case you don't need to chain the default constructor:

 public S(int x)
 {
     this.X = x;
     /* custom initialization logic */
 }

Another option is to skip calling a constructor, and instead initialize the struct manually, just before you actually use it:

S s;
s.X = 5;

Out of the above options, only the first performs a logical call to the default constructor, while the other two skip the call.

The bad news is that, behind the scenes, the CLR will always give you zero-initialized memory for safety reasons, which is pretty much exactly equivalent to calling the C# default constructor. You can observe it like this:

S s;
S* ptr = &s;
int x = (*ptr).X;
Console.WriteLine(x); // will print 0

The good news is that, for most cases, this doesn't matter. The C# default constructors aren't really constructors in the CLR sense. The expression new T() for any T : struct is equivalent to default(T), which is simply a way to express getting a zero-initialized instance of a struct of type T. It's also why you can't make it private or add custom logic to it.

Therefore, C# default struct constructors cannot have any logical externally observable side-effects. Which brings us to a question: what kinds of effects are you trying to avoid?

Keep in mind that if your problem is that your struct is invalid for the all-zero bit pattern and you're afraid that some client code might attempt to use it like that, there is a workaround:

struct S
{
    private bool _isValid;
    /* rest of your struct */
}

Any zero-initialized struct will have its validity flag not set, and you can use that to throw exceptions as soon as any operation (that you can control) is performed on your struct.

After all this, if for any reason you still need to a struct instance that has somehow skipped calling any constructor (whether a real or an apparent one), you'll have to allocate some memory for the struct yourself:

S* ptr = (S*) Marshal.AllocHGlobal(Marshal.SizeOf<S>());

You can now use expressions of the form (*ptr).X or ptr->X to read and write to that struct directly.

6
  • I need to forbid the default constructor (along with any other possible default instantiation method, like the default(T) you said) to be called because for these specific custom types I need to run some initiation code right after the object is created in memory - before any other code gets the reference. So, what I understood from what you said is that what I want to accomplish is impossible because there are other ways to create an instance besides the default constructor; is that right? Thank you very much! Apr 13, 2016 at 17:28
  • 1
    @ptr0x In C#, if a struct exists, then zero-initialization can be performed by anyone. Even if you hid the default constructor, someone could still do new S[1] and get one zero-initialized instance of your struct. I can't think of any easy way out of this. Maybe if your public struct is effectively just a handle to the real struct, and the real struct is kept internal? I'm just speculating. Apr 13, 2016 at 17:32
  • Okay, and if I change these types from struct to classes, making the default constructor private is enough to forbid any way of default initialization? Apr 13, 2016 at 17:35
  • 1
    @ptr0x Definitely. default(T) is null for all classes and no constructor is necessary. But if you want to maintain blittable types, you can still go for the hidden validity flag. Apr 13, 2016 at 17:36
  • Got it. I can't maintain these types blittable anymore. Tried so hard to keep that way, but I just can't find a viable way to do this. I'm developing a mmorpg server in C# and I need to make some high-level representation of the packet data. My initial thoughts were to make blittable structs of these packets and just make a pointer cast (to struct* from byte[]), but this can not be done in my case. Apr 13, 2016 at 17:56
2

As Aybe specified you cannot do it but it you want you can add a field in the struct like IsInitialized that will default to false and make the code using it to verify the value and throw if false.

1
  • Nice apointment. Making a class's default constructor private I ensure it will not be called at ANY circumstances? Thanks! Apr 13, 2016 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.