VB.Net 关于类型关键字加方括号进行转义(Dim As [String])的总结

前端之家收集整理的这篇文章主要介绍了VB.Net 关于类型关键字加方括号进行转义(Dim As [String])的总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

看到帖子《Dim s1 As [String]和Dim s1 As String之间什么区别》的提问,专门做了测试,总结如下。

一、本来用方括号转义名称是为了类似下面这样的用法
1)把关键字作为类型。
2)把关键字作为变量。

  1. Module Module1
  2.  
  3. Private Enum [String]
  4. ASCII
  5. Unicode
  6. End Enum
  7.  
  8. Dim s As [String] = [String].Unicode
  9.  
  10. Dim [As] As Integer = 0
  11.  
  12. End Module


二、当 As 后面出现类型关键字加方括号的转义,并且这个转义类型没有在前面定义过时:
A)标准的 CLR 类型会自动解释为原始的类型(当作方括号不存在)。
B)而 VB.Net 的特有类型会报错。

根据《数据类型摘要 (Visual Basic)》,把基本类型都转义了一下,测试代码如下:

  1. Module Module2
  2.  
  3. Dim a As [Char]
  4. Dim b1 As [DateTime],b2 As [Date]
  5. Dim c As [Decimal]
  6. Dim d As [Double]
  7. Dim e1 As [Int32],e2 As [Integer]
  8. Dim f1 As [Int64],f2 As [Long]
  9. Dim g As [Object]
  10. Dim h As [SByte]
  11. Dim g1 As [Int16],g2 As [Short]
  12. Dim i As [Single]
  13. Dim j As [String]
  14. Dim k1 As [UInt32],k2 As [UInteger]
  15. Dim l1 As [UInt64],l2 As [ULong]
  16. Dim m1 As [UInt16],m2 As [UShort]
  17.  
  18. End Module

在 Visual Studio .Net (截图)中可以看到编译错误

猜你在找的VB相关文章