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?

2 comments

@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

45 Answered Questions

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

  • 2013-08-07 03:02:46
  • Stephen C
  • 2481796 View
  • 1255 Score
  • 45 Answer
  • Tags:   java class main

26 Answered Questions

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

14 Answered Questions

[SOLVED] ES6 class variable alternatives

13 Answered Questions

28 Answered Questions

[SOLVED] When to use struct?

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

11 Answered Questions

10 Answered Questions

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

  • 2008-12-02 12:39:25
  • Motti
  • 99249 View
  • 239 Score
  • 10 Answer
  • Tags:   c# .net struct

5 Answered Questions

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

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

4 Answered Questions

Sponsored Content