0

I am working with vb.net I noitce that there are somtimes :

Dim broadcastBytes As Byte()

and

Dim broadcastBytes As [Byte]

Is there a diffrence or its just a syntax ?

MatSnow
  • 7,357
  • 3
  • 19
  • 31
  • 1
    Does this answer your question? [What do square brackets around an identifier in VB.NET signify?](https://stackoverflow.com/questions/6413343/what-do-square-brackets-around-an-identifier-in-vb-net-signify) – GoWiser Sep 08 '21 at 09:42
  • I think that the answers in this post are betters and detailed – Idan Azari Sep 08 '21 at 11:44

2 Answers2

3

Yes, there is a difference:

Dim broadcastBytes As Byte() ➔ Declares the variable as a Byte - Array:

Dim broadcastBytes As [Byte] ➔ Here [Byte] is just the datatype Byte, but declared with square brackets, which is actually not required here.
(see here: What do square brackets around an identifier in VB.NET signify? )

MatSnow
  • 7,357
  • 3
  • 19
  • 31
1

MatSnow already answered your immediate question, let me add a few more cases you might encounter for completeness.

Note, in particular, that New Byte() can mean either (a) a new byte or (b) a new byte array, depending on whether it is followed by {...} nor not.

' 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

In all of these cases, Byte can be replaced by [Byte] without changing anything.

Heinzi
  • 167,459
  • 57
  • 363
  • 519