Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Boolean and [Boolean]?

I ran some code through an automatic translator for C# to VB, and it translated some code like this:

Public Property Title As [String]

How is this different to

Public Property Title As String

and why do both exist?

like image 901
NibblyPig Avatar asked Dec 13 '22 23:12

NibblyPig


1 Answers

String is a keyword. If you want to use a keyword as an identifier, you'll have to enclose it in brackets. [String] is an identifier. String keyword always refers to System.String class while [String] can refer to your own class named String in the current namespace. Assuming you have Imports System, both refer to the same thing most of the time but they can be different:

Module Test
    Class [String]
    End Class
    Sub Main()
        Dim s As String = "Hello World"        // works
        Dim s2 As [String] = "Hello World"     // doesn't work
    End Sub
End Module

The primary reason for existence of [ ] for treating keywords as identifiers is interoperability with libraries from other languages that may use VB keywords as type or member names.

like image 88
mmx Avatar answered Jan 14 '23 00:01

mmx