看到帖子《Dim s1 As [String]和Dim s1 As String之间什么区别》的提问,专门做了测试,总结如下。
一、本来用方括号转义名称是为了类似下面这样的用法:
1)把关键字作为类型。
2)把关键字作为变量。
Module Module1
Private Enum [String]
ASCII
Unicode
End Enum
Dim s As [String] = [String].Unicode
Dim [As] As Integer = 0
End Module
二、当 As 后面出现类型关键字加方括号的转义,并且这个转义类型没有在前面定义过时:
A)标准的 CLR 类型会自动解释为原始的类型(当作方括号不存在)。
B)而 VB.Net 的特有类型会报错。
根据《数据类型摘要 (Visual Basic)》,把基本类型都转义了一下,测试代码如下:
Module Module2
Dim a As [Char]
Dim b1 As [DateTime], b2 As [Date]
Dim c As [Decimal]
Dim d As [Double]
Dim e1 As [Int32], e2 As [Integer]
Dim f1 As [Int64], f2 As [Long]
Dim g As [Object]
Dim h As [SByte]
Dim g1 As [Int16], g2 As [Short]
Dim i As [Single]
Dim j As [String]
Dim k1 As [UInt32], k2 As [UInteger]
Dim l1 As [UInt64], l2 As [ULong]
Dim m1 As [UInt16], m2 As [UShort]
End Module
在 Visual Studio .Net (截图)中可以看到编译错误