Visual Basic 9 syntax for incomplete entry sugar

Source: Internet
Author: User
Tags array definition integer

Introduction

In Visual Basic 9 Beta 1, there are some "trivial" syntactic sugars with the weight-enhancing enhancements of LINQ. These syntactic sugars do not affect the final compiled IL, but are sufficient to ease the programmer's workload and achieve more efficient and easy development.

What are the grammatical sugars?

1, local variable type conjecture

2. Array initializers

3, Object initialization device

is the grammar candy good?

Personally, these grammatical sugars are still quite tasty. The object initializer is very good; the local variable type is also quite useful to speculate.

How to eat grammar candy?

1, local variable type conjecture

Before Visual Basic 9, only a strongly typed object could be defined with the AS statement. In VB 9, the type declaration of a local variable is allowed, determined by the initialization statement. See Example:

1'Visual Basic 9 之前
2Dim List As List(Of String) = New List(Of String)
3'Visual Basic 9
4Dim List = New List(Of String)

Don't worry about performance problems; it's compiled by the compiler and is a strongly typed feature.

At the same time, VB 9 also supports a for-for-Each loop variable conjecture, no need to temporarily define the loop variable.

1Dim Sample As Integer(5)
2'Visual Basic 9 之前
3For Each I As Integer In Sample
4 
5Next
6'Visual Basic 9
7For Each I In Sample
8 
9Next

2. Array initializers

Visual Basic 9 introduces a simplified array definition method that can help programmers reduce their code workload. Very simple, look at the code:

1'Visual Basic 9 以前
2Dim OldArr As Integer() = New Integer(){1, 2, 3}
3'Visual Basic 9
4Dim NewArr As Integer() = {1, 2, 3}

3, Object initialization device

Object initializers are an important syntax enhancement, which is of great help to coding. As we know, in visual Basic 8 and previous versions, you had to create a class by assigning a property one at a time. In Visual Basic 9, everything is different:

'Visual Basic 8
Dim OldArea As New Area
With OldArea
.Subject = "北京"
End With
'Visual Basic 9
Dim NewArea As New Area With {.Subject = "北京"}

This kind of grammatical expression expands the content that "expression" can express, as if the WITH statement is put into the initialization statement generally. From then on, many functions that would otherwise require several lines of code can be integrated into one sentence complete.

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.