Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default value of a variable at the time of declaration in C# and VB?

Can anybody tell me what is the default value of a variable at the time of declaration in C# and vb??


2 Answers

In c# you can use the default keyword to determine default values.

For example:

default(bool)
default(int)
default(int?)
like image 51
Chris Avatar answered Sep 11 '25 02:09

Chris


Do you mean a (method) variable? or a field (on an instance or type)?

For a method-level variable (in C# at least) it is irrelevant, since "definite assignment" means that you must give it a value before you can read it.

Fields default to the bitwise zero state:

  • for reference types (including string) that means null
  • for Nullable<T> (int? etc) that means null
  • for numerics that means 0
  • for enums that means 0 even if there is no 0-valued enum defined
  • for bools that means false
  • for DateTime, that means the same as MinValue
  • for other structs, you'll have to check their documentation, but it will be a (hopefully sensible) "zero/empty" value
like image 28
Marc Gravell Avatar answered Sep 11 '25 00:09

Marc Gravell