C# 和 VB 中声明时变量的默认值?

发布于 2024-07-14 09:05:24 字数 35 浏览 8 评论 0原文

谁能告诉我 C# 和 VB 中声明时变量的默认值是什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦旅人picnic 2024-07-21 09:05:24

在 C# 中,您可以使用 default 关键字来确定默认值。

例如:

default(bool)
default(int)
default(int?)

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

For example:

default(bool)
default(int)
default(int?)
Saygoodbye 2024-07-21 09:05:24

您的意思是(方法)变量吗? 或字段(在实例或类型上)?

对于方法级变量(至少在 C# 中)来说,这是无关紧要的,因为“明确赋值”意味着您必须先给它一个值,然后才能读取它。

字段默认为按位零状态:

  • 对于引用类型(包括字符串),这意味着 null
  • 对于 Nullableint? 等),这意味着 null
  • 对于数字,意味着 0
  • 对于枚举,这意味着 0 即使没有定义 0 值枚举
  • 对于 bool,这意味着 DateTime 为 false
  • ,这意味着与其他结构的 MinValue 相同
  • ,您必须检查他们的文档,但是它将是一个(希望是合理的)“零/空”值

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
再见回来 2024-07-21 09:05:24

这可以在 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

南笙 2024-07-21 09:05:24

C# 语言规范规定,对于值类型,默认值与默认构造函数分配的值相同,而对于引用类型,则默认值为 null:

因此值类型默认构造函数值为:

对于所有简单类型,默认
value 是一个位产生的值
全零的模式:

  • 对于 sbyte、byte、short、ushort、int、uint、long 和 ulong,
    默认值为 0。

  • 对于 char,默认值为“\x0000”。 对于浮动,默认
    值为 0.0f。

  • 对于 double,默认值为
    0.0d。 对于十进制,默认值为
    0.0米。 对于 bool,默认值为 false。

  • 对于 enum-typeE,默认值为 0。

  • 对于结构类型,默认值是由
    将所有值类型字段设置为
    他们的默认值和所有
    将类型字段引用为 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:

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.

半葬歌 2024-07-21 09:05:24

取决于变量的类型。 如果类型可以为 null,那么它的默认值将为 null。 可空类型都将从 null 开始。

  • 复杂类型 (String、StringBuilder) = null
  • 数字类型 (int、decimal、double、byte) = 0
  • Boolean = false
  • DateTime = DateTime.MinValue (01/01/0001 00:00:00)

您可以使用以下方式设置初始值:

字符串 s1 = "测试";

将 s1 调暗为字符串 =“测试”

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文