Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
746 views
in Technique[技术] by (71.8m points)

syntax - 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:

Me.[GetType]()

What is the purpose of the brackets around GetType?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...