Leigha Brown
Leigha Brown

Reputation: 41

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

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?

Upvotes: 4

Views: 552

Answers (2)

O. R. Mapper
O. R. Mapper

Reputation: 20720

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);

Upvotes: 5

Joe Sewell
Joe Sewell

Reputation: 6610

Like Johnny mentioned in the comments, this table lists the default values for .NET types. The default value of a reference-type field is null.

Upvotes: 6

Related Questions