Module Module1
Sub Main ()
'Original here: Visual Basic 9 Incomplete entry series (2): syntactic sugar http://www.cnblogs.com/cangying/archive/2007/06/02/765527.html
'What are syntactic sugar?
'1. Prediction of local variable types
'2. array initializer
'3. Object initiator'
'Next let's take a look at it. First, we guess the type of local variables.
Before Dim aList As List (Of String) = New List (Of String) 'vb9, you can only use the As statement to define a strongly typed object.
In Dim bList = New List (Of String) 'vb9, the local variable type declaration can be exempted, determined by the initialization statement.
'Don't worry about performance: It is speculative by the compiler during compilation, and is a strong type of feature.
'Vb9 also supports loop variable speculation for (for each), without the need to temporarily define the Loop Variable
Dim Sample () As Integer = {1, 2, 3, 4, 5}
'Before vb9
For Each I As Integer In Sample
Console. WriteLine (I)
Next
'Vb9
For Each k In Sample
Console. Write (k &",")
Next
'2. array initializer
'Before vb9
Dim oldvb9 As Integer () = New Integer () {0, 9, 8}
'Vb9
Dim newvb9 As Integer () = {5, 6, 7}
'3. Object initiator'
'This is an important syntax enhancement. Before vb8, attributes must be assigned values one by one.
'Before vb9
Dim oldArea As New Area
With oldArea
. Subject = "Beijing"
End
'Vb9
Dim newArea As New Area With {. Subject = "Beijing "}
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