VBNET之VB9语法糖
Module Module1
Sub Main()
'原文在这里:Visual Basic 9 不完全入门系列(2) : 语法糖 http://www.cnblogs.com/cangying/archive/2007/06/02/765527.html
'语法糖有哪些?
'1.局部变量类型推测
'2.数组初始化器
'3.对象初始化器
'下面具体看,第一个,局部变量类型推测
Dim aList As List(Of String) = New List(Of String) 'vb9之前,只能用As语句定义强类型对象
Dim bList = New List(Of String) 'vb9中,允许免除局部变量类型声明,由初始化语句确定
'不要担心性能问题:它是编译时由编译器进行推测的,是一种强类型的特性.
'vb9还支持for(for each)的循环变量推测,无需再临时定义循环变量
Dim Sample() As Integer = {1, 2, 3, 4, 5}
'vb9之前
For Each i As Integer In Sample
Console.WriteLine(i)
Next
'vb9
For Each k In Sample
Console.Write(k & ", ")
Next
'2.数组初始化器
'vb9之前
Dim oldvb9 As Integer() = New Integer() {0, 9, 8}
'vb9
Dim newvb9 As Integer() = {5, 6, 7}
'3.对象初始化器
'这是一项重要的语法增强,在vb8以前,属性必须逐个赋值
'vb9之前
Dim oldArea As New Area
With oldArea
.Subject = "北京"
End With
'vb9
Dim newArea As New Area With {.Subject = "北京"}
Console.ReadKey()
End Sub
Public Class Area
Private _subject As String
Public Property Subject()
Get
Return _subject
End Get
Set(ByVal value)
_subject = value
End Set
End Property
End Class
End Module