What are the "default values" that .Net sets all of the values in a class to if the class does not have a constructor?

Viewed 348

In c# using .Net, if you create a class with an uninitialized field and without a constructor, then create an instance of the class with the new keyword, .Net sets the value of the field to the "default value". What exactly does that mean? Is there ever a situation where the value would be set to Null?

2 Answers

The default value is defined on a per-type basis. In general, any reference type will default to null.

You can find a full list of default values based on the type in the documentation.

Furthermore, you can find out empirically by explicitly using the default keyword and checking (e.g. in the debugger) what value was returned:

var x = default(string);
var y = default(int);
Related