views:

5368

answers:

5

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

+11  A: 

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

For example:

default(bool)
default(int)
default(int?)
Chris
Nice "generic" way to get the information. Should be accepted as answer !
Julien N
+3  A: 

Depends on the type of the variable. If the type can be null then it's default value will be null. Nullable types will all start null.

  • Complex types (String, StringBuilder) = null
  • Numeric types (int, decimal, double, byte) = 0
  • Boolean = false
  • DateTime = DateTime.MinValue (01/01/0001 00:00:00)

You can set an initial value using:

string s1 = "test";

Dim s1 As String = "test"

Duke of Muppets
+4  A: 

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
Marc Gravell
+2  A: 

This can be found in MSDN:

Visual Basic .NET defines the following primitive types:

The integral value types Byte (1-byte unsigned integer), Short (2-byte signed integer), Integer (4-byte signed integer), and Long (8-byte signed integer). These types map to System.Byte, System.Int16, System.Int32, and System.Int64, respectively. The default value of an integral type is equivalent to the literal 0.

The floating-point value types Single (4-byte floating point) and Double (8-byte floating point). These types map to System.Single and System.Double, respectively. The default value of a floating-point type is equivalent to the literal 0.

The Decimal type (16-byte decimal value), which maps to System.Decimal. The default value of decimal is equivalent to the literal 0D.

The Boolean value type, which represents a truth value, typically the result of a relational or logical operation. The literal is of type System.Boolean. The default value of the Boolean type is equivalent to the literal False.

The Date value type, which represents a date and/or a time and maps to System.DateTime. The default value of the Date type is equivalent to the literal # 01/01/0001 12:00:00AM #.

The Char value type, which represents a single Unicode character and maps to System.Char. The default value of the Char type is equivalent to the constant expression ChrW(0).

The String reference type, which represents a sequence of Unicode characters and maps to System.String. The default value of the String type is a null reference.

http://msdn.microsoft.com/en-us/library/aa711900.aspx

Billy
Thanks everyone.. My problem has been resolved. Your responses are appreciated.
A: 

The C# language specification states that for value types the default value is the same as the one assigned by the default constructor and for reference types it is null:

So the value types default constructor values are:

For all simple-types, the default value is the value produced by a bit pattern of all zeros:

  • For sbyte, byte, short, ushort, int, uint, long, and ulong, the default value is 0.

  • For char, the default value is '\x0000'. For float, the default
    value is 0.0f.

  • For double, the default value is 0.0d. For decimal, the default value is 0.0m. For bool, the default value is false.

  • For an enum-typeE, the default value is 0.

  • For a struct-type, the default value is the value produced by setting all value type fields to their default value and all reference type fields to null.

http://msdn.microsoft.com/en-us/library/aa691142(v=VS.71).aspx

The string is not a value type.

pre63