Gaurav Moolani
Gaurav Moolani

Reputation: 362

C# Value type Initialization mystery

http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx

in the above mentioned article i read . "Each value type has an implicit default constructor that initializes the default value of that type."

But in this article Jon Skeet says

Why can't I define a default constructor for a struct in .NET?

"the basic rule in C# is "the default value for any type can't rely on any initialization" means as mentioned in comments. The default value is always "what you'd get by wiping memory". You can't provide any explicit implementation which will always be called.

Can anybody tell me who is correct ?

what actually happens when a value type is declared its constructor is called or memory is wiped out.

Upvotes: 0

Views: 568

Answers (3)

Cole Francis
Cole Francis

Reputation: 1

It's really no mystery at all. All value types are initialized using an implicit default constructor that is responsible for assigning a default value to the type. In fact, here's a link that shows the default values for each value type.

https://msdn.microsoft.com/en-us/library/83fhsxwc.aspx

Upvotes: 0

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

what actually happens when a value type is declared its constructor is called or memory is wiped out.

Lets say you have the following:

public struct Foo
{
    public int A { get; set; }
    public object B { get; set; }
}

and you allocate a Foo:

var f = new Foo();

The compiler emits a call to initobj:

IL_0001:  ldloca.s    00 // f
IL_0003:  initobj     UserQuery.Foo

Initializes each field of the value type at a specified address to a null reference or a 0 of the appropriate primitive type

And goes on to describe how they get initialized:

The initobj instruction initializes each field of the value type specified by the pushed address (of type native int, &, or *) to a null reference or a 0 of the appropriate primitive type. After this method is called, the instance is ready for a constructor method to be called. If typeTok is a reference type, this instruction has the same effect as ldnull followed by stind.ref. Unlike Newobj, initobj does not call the constructor method. Initobj is intended for initializing value types, while newobj is used to allocate and initialize objects.

All this actually convies the fact the value types do get initialized (as it seems that from your question you may question that statement).

Note that as of C# 6.0, you can declare a default parameterless constructor for a value type. If you do, it will be invoked if you explicitly instantiate your value type via the new operator.

Mads Torgersen explains:

The expression ‘new Person()’ will execute the declared constructor instead of the standard behavior of providing the default value. Note, however, that ‘default(Person)’ will still produce the default value, as will ‘new Person[…]’ for each of the array elements. In those cases the constructor is not executed: It’s only when you explicitly use ‘new’ on the struct type.

Who wipes it out is the same for all managed CLR types, the garbage collector. It will notice that it is no longer pointed to, and will collect the type and do its magic.

Upvotes: 3

CodeCaster
CodeCaster

Reputation: 151604

Both links don't contradict each other. It would help if you could explain why you think so.

You seem to think "the default value for any type can't rely on any initialization" means "value types don't get initialized", which is not true. They get initialized to 0.

What Jon means to say there (and does say) is "Users can't provide a custom constructor for value types".

Upvotes: 2

Related Questions