简体   繁体   English

VB.NET中标识符的方括号是什么意思?

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

I'm quite familiar with VB and .NET in general, but I just ran across this code: 我对VB和.NET非常熟悉,但我只是遇到了这段代码:

Me.[GetType]()

What is the purpose of the brackets around GetType ? 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() . 但是您的示例应与Me.GetType()相同。

You could use it for example for Enums. 您可以将它用于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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM