开发者

What does [String] mean in VB.NET?

Does开发者_StackOverflow中文版 anybody know what does the following construct mean:

    Dim s1 as [String]

What do the square brackets mean? And why does the following statement with Integer fail while the one above, with String works?

    Dim i1 as [Integer]

Thanks in advance.


THe square brackets is used so that the compiler interprets it as a type, even if it would be a keyword. Imagine for example if you had a class named As:

Dim a As [As]

This is usually only used in auto generated code, so that it works with any type that you throw at it.

The reason that you can't use [Integer] is that Integer is not a data type, it's a keyword. You would have to use the corresponding data type, i.e. [Int32].


Square brackets are used to create a variable that has the same name as a keyword in VB.NET. So they are more often used that way:

Dim [Integer] As Integer
Dim [String] As String


In addition to the other answers:

For the case of using variable names with names the same as types: you shouldn't have to use those in your own code. If you do, you are naming your variable names incredibly poorly, and need to work on using better variable names first!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜