Can anybody tell me what is the default value of a variable at the time of declaration in C# and vb??
相关问题
- Batch - Set variables in for loop
- C# How do I create code to set a form back to defa
- JavaScript, confusing syntax when declaring a vari
- How can I create a new variable based on condition
- Why is a variable not updating after changing its
相关文章
- Loss of variables switching orientations
- What is the limit in size of a Php variable when s
- Are default parameters bad practice in OOP?
- Makefile and use of $$
- Splitting an array into the variables in php
- javascript setInterval() and Variable Scope
- Change default package from com.example for Eclips
- Initialization in polymorphism of variables
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.
You can set an initial value using:
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
In c# you can use the default keyword to determine default values.
For example:
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:
Nullable<T>
(int?
etc) that means nullThe 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:
http://msdn.microsoft.com/en-us/library/aa691142(v=VS.71).aspx
The string is not a value type.