VBNET VB9 syntax sugar

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.