By Leigha Brown

2019-10-09 14:26:07 8 Comments

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?


@O. R. Mapper 2019-10-09 14:31:37

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

@Joe Sewell 2019-10-09 14:31:37

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.

@JosephDoggie 2019-10-09 14:36:03

Thanks for the table, very interesting! (In the old days, I'd make it a 'favorite', but that's getting more obsolete, now)....

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] Why not inherit from List<T>?

46 Answered Questions

[SOLVED] What does "Could not find or load main class" mean?

  • 2013-08-07 03:02:46
  • Stephen C
  • 2697125 View
  • 1368 Score
  • 46 Answer
  • Tags:   java class main

14 Answered Questions

[SOLVED] ES6 class variable alternatives

4 Answered Questions

14 Answered Questions

11 Answered Questions

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

  • 2008-12-02 12:39:25
  • Motti
  • 108580 View
  • 257 Score
  • 11 Answer
  • Tags:   c# .net struct

28 Answered Questions

[SOLVED] When to use struct?

  • 2009-02-06 17:37:55
  • Alex Baranosky
  • 271752 View
  • 1389 Score
  • 28 Answer
  • Tags:   c# struct

11 Answered Questions

5 Answered Questions

[SOLVED] Does C# 6.0 work for .NET 4.0?

  • 2015-03-08 00:22:27
  • MajesticRa
  • 71774 View
  • 274 Score
  • 5 Answer
  • Tags:   c# .net c#-6.0

Sponsored Content