Home > OS >  Difference between [Byte] and byte() [duplicate]
Difference between [Byte] and byte() [duplicate]

Time:09-16

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 ?

CodePudding user response:

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? )

CodePudding user response:

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.

  • Related