开发者

What do square brackets around an identifier in VB.NET signify?

开发者 https://www.devze.com 2023-03-14 08:07 出处:网络
I\'m quite familiar with VB and .NET in general, but I just ran across this code: Me.[GetType]() What is the purpose of the bracket开发者_StackOverflows around GetType?The square brackets are used

I'm quite familiar with VB and .NET in general, but I just ran across this code:

Me.[GetType]()

What is the purpose of the bracket开发者_StackOverflows around GetType?


The square brackets are used to tell the compiler that he should interpret it as a type, even if it would be a keyword. But your example should be the same as Me.GetType().

You could use it for example for Enums.

Example-Enum:

Enum Colors
    Red
    Green
    Blue
    Yellow
End Enum 'Colors

Dim colors = [Enum].GetValues(GetType(Colors))
For Each c In colors
   Console.WriteLine(c)
Next

That wouldn't compile normally:

Enum.GetValues(GetType(Colors)) 'because Enum is a keyword'
0

精彩评论

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