【发布时间】:2021-11-05 01:32:54
【问题描述】:
我正在使用 vb.net 我注意到有时会有:
Dim broadcastBytes As Byte()
和
Dim broadcastBytes As [Byte]
有区别还是只是语法?
【问题讨论】:
-
我认为这篇文章的答案更好更详细
我正在使用 vb.net 我注意到有时会有:
Dim broadcastBytes As Byte()
和
Dim broadcastBytes As [Byte]
有区别还是只是语法?
【问题讨论】:
是的,有区别:
Dim broadcastBytes As Byte() ➔ 将变量声明为Byte - Array:
Dim broadcastBytes As [Byte] ➔ 这里[Byte]只是数据类型Byte,但是用方括号声明,这里实际上不需要。
(见这里:What do square brackets around an identifier in VB.NET signify? )
【讨论】:
MatSnow 已经回答了您的直接问题,为了完整起见,让我再补充几个您可能会遇到的案例。
请特别注意,New Byte() 可以表示 (a) 一个新字节或 (b) 一个新字节数组,这取决于它后面是否跟 {...}。
' All examples assume Option Strict and Option Infer On (as it should be)
Dim b As Byte ' uninitialized Byte variable
Dim b As Byte() ' uninitialized Byte-Array (Nothing)
Dim b() As Byte ' same as above, legacy notation
Dim b As New Byte() ' initialized Byte variable (0), explicitly calling the default constructor
Dim b As Byte = New Byte() ' same as above
Dim b = New Byte() ' same as above, with type inference
Dim b As Byte = 0 ' same as above, initialized with a literal
Dim b = CByte(0) ' same as above, with type inference
Dim b As New Byte() {} ' initialized Byte-Array (not Nothing, zero elements)
Dim b As New Byte() {1, 2} ' initialized Byte-Array (two elements)
Dim b = New Byte() {1, 2} ' same as above, with type inference
在所有这些情况下,Byte 可以替换为 [Byte] 而无需更改任何内容。
【讨论】: