【问题标题】:Difference between [Byte] and byte() [duplicate][Byte] 和 byte() 之间的区别 [重复]
【发布时间】:2021-11-05 01:32:54
【问题描述】:

我正在使用 vb.net 我注意到有时会有:

Dim broadcastBytes As Byte()

Dim broadcastBytes As [Byte]

有区别还是只是语法?

【问题讨论】:

标签: arrays vb.net syntax


【解决方案1】:

是的,有区别:

Dim broadcastBytes As Byte() ➔ 将变量声明为Byte - Array

Dim broadcastBytes As [Byte] ➔ 这里[Byte]只是数据类型Byte,但是用方括号声明,这里实际上不需要。
(见这里:What do square brackets around an identifier in VB.NET signify?

【讨论】:

    【解决方案2】:

    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] 而无需更改任何内容。

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 2021-11-23
      • 2019-05-27
      • 2011-10-15
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 2014-08-16
      相关资源
      最近更新 更多