C# 和 VB 中声明时变量的默认值?
谁能告诉我 C# 和 VB 中声明时变量的默认值是什么?
Can anybody tell me what is the default value of a variable at the time of declaration in C# and vb??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 C# 中,您可以使用 default 关键字来确定默认值。
例如:
In c# you can use the default keyword to determine default values.
For example:
您的意思是(方法)变量吗? 或字段(在实例或类型上)?
对于方法级变量(至少在 C# 中)来说,这是无关紧要的,因为“明确赋值”意味着您必须先给它一个值,然后才能读取它。
字段默认为按位零状态:
Nullable
(int?
等),这意味着 nullDo 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 null这可以在 MSDN 中找到:
Visual Basic .NET 定义了以下基元类型:
整数值类型 Byte(1 字节无符号整数)、Short(2 字节有符号整数)、Integer(4 字节有符号整数)和 Long (8 字节有符号整数)。 这些类型分别映射到 System.Byte、System.Int16、System.Int32 和 System.Int64。 整型的默认值相当于文字 0。
浮点值类型 Single(4 字节浮点)和 Double(8 字节浮点)。 这些类型分别映射到 System.Single 和 System.Double。 浮点类型的默认值相当于文字 0。
Decimal 类型(16 字节十进制值),映射到 System.Decimal。 十进制的默认值相当于文字 0D。
布尔值类型,表示真值,通常是关系或逻辑运算的结果。 该文字的类型为 System.Boolean。 Boolean 类型的默认值相当于文字 False。
Date 值类型,表示日期和/或时间并映射到 System.DateTime。 Date 类型的默认值相当于文字 # 01/01/0001 12:00:00AM #。
Char 值类型,表示单个 Unicode 字符并映射到 System.Char。 Char 类型的默认值相当于常量表达式 ChrW(0)。
String 引用类型,表示 Unicode 字符序列并映射到 System.String。 String 类型的默认值为空引用。
http://msdn.microsoft.com/en-us/library/aa711900.aspx
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
C# 语言规范规定,对于值类型,默认值与默认构造函数分配的值相同,而对于引用类型,则默认值为 null:
因此值类型默认构造函数值为:
http://msdn.microsoft.com/en- us/library/aa691142(v=VS.71).aspx
字符串不是值类型。
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:
http://msdn.microsoft.com/en-us/library/aa691142(v=VS.71).aspx
The string is not a value type.
取决于变量的类型。 如果类型可以为 null,那么它的默认值将为 null。 可空类型都将从 null 开始。
您可以使用以下方式设置初始值:
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: