3

I read somewhere that using new to instantiate a struct assigns default values to all of the instance's variables. Does it imply that a struct will have its values initialized through a default hidden constructor?

4 Answers 4

3

Does it imply that a struct will have its values initialized through a default hidden constructor?

Yes, in the sense that all member fields will be set to their default(T) values.

In practice, the compiler could just do something like memset(ptr, 0, sizeInBytes) . What actually happens is an implementation detail.

Sign up to request clarification or add additional context in comments.

2 Comments

What happens when you skip the new keyword?
The compiler will treat all public fields as unassigned. You'll have to set them one by one. When you have properties the struct will be unusable.
0

Struct can not have a parameterless constructor and in order to initialize your values you have to write a constructor with parameters and call it explicitly on object initialization and you have to initialize all fields, and by default if you didn't specify a constructor all values will be initialized to their default values like you said with an invisible one. Note: the CLR does allow Structs to have a parameterless constructor in IL language for example.

Comments

0

Every type of storage location in .NET which occupies N bytes has a default value, whose representation is a sequence of N bytes initialized to zero. The default value of a any reference type will behave as a null reference, the default value of any numeric type will behave as the number zero, and the default value of a structure type will behave as an instance whose members all have default types. The storage location is initialized to the default value when the object that contains it is created; the only aspect of the storage location's type which is relevant to the process is its size. If a structure type Foo is 16 bytes long and a class has a field of type Foo, creating an instance of that class will reserve 16 bytes for the structure and initialize them to zero, without calling any kind of constructor for that structure.

Note that if a struct has a parameterless constructor, there are some circumstances in which creating an instance will invoke that constructor, but many in which creating a storage location will effectively create an instance without calling the constructor. Because it's not always clear which treatment would be applicable in which situations, some language designers have decided to dodge the issue by simply forbidding the definition of parameterless constructors and assuming that no structures will have them (ignoring the possibility of parameterless constructors declared in other languages).

3 Comments

Implicit in your explanation: A struct created by the C# compiler does not have such a parameterless instance constructor. So that last paragraph is not relevant for such a struct.
@JeppeStigNielsen: I haven't been following the evolution of C#. I know there was some talk of allowing parameterless struct constructors, but don't know if anything ever became of those. Personally, I dislike the notion of language designers deciding what features of the underlying platform programmers are allowed to access, especially for languages like C# which are supposed to be "universal". I think it's ironic, for example, that vb.net exception handling was for years semantically superior to that of C# because it could [albeit awkwardly] have a finally clause...
...behave differently based upon whether the try clause exited normally, without having to catch the exception. IMHO, the proper model of exception handling in most cases should be that an unanticipated exception that occurs while an object could be in an invalid state should leave it in an expressly-invalidated state, but the presence of code that invalidates the object should not cause the exception to be considered "handled".
0

My "workaround" to use structures with a parameterless constructor

struct MyStruc
{
    // MyStruc( int NoOp = 0 ) did not work. The code was not called until a parameter was passed in. 
    public MyStruc( int NoOp )
    {
       MyFooBar = new FooBar()
    }

    public FooBar MyFooBar;
}

public class Main
{
    // Implementation
    public MyStruc ms = new MyStruct(0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.