Hide parameterless constructor on struct

No, you can't do this. As you said, similar question has been asked before - and I thought the answer was fairly clear that you couldn't do it.

You can create a private parameterless constructor for a struct, but not in C#. However, even if you do that it doesn't really help - because you can easily work around it:

MyStruct[] tmp = new MyStruct[1];
MyStruct gotcha = tmp[0];

That will be the default value of MyStruct - the "all zeroes" value - without ever calling a constructor.

You could easily add a Validate method to your struct and call that each time you received one as a parameter, admittedly.


Nope can't hide it. Structs cannot redefine zero arg constructor, so therefore its visibility can't be redefined.

Tags:

C#

.Net

Struct