Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String vs [String] in VB.Net

Tags:

string

vb.net

I'm wondering what the difference is when declaring variables as such...

Dim something as String = Nothing

and

Dim something as [String] = String.Empty

Specifically the difference between String and [String].

like image 549
Gil Avatar asked May 19 '14 15:05

Gil


1 Answers

There is no difference, apart from the fact that the first is Nothing and the second is an empty string.

You use the square brackets([...]) around an identifier in VB.NET to tell the compiler that it should ignore a keyword.

It is often used on Enum:

Dim colors = [Enum].GetValues(GetType(Colors))

since Enum is a keyword and a type. Without the brackets that wouldn't compile.

MSDN:

Regular identifiers may not match keywords, but escaped identifiers can. An escaped identifier is an identifier delimited by square brackets.

like image 86
Tim Schmelter Avatar answered Oct 11 '22 15:10

Tim Schmelter