Visual Basic 9 Syntax enhancements for incomplete entry

Source: Internet
Author: User
Tags extend static class thread

Why is it enhanced?

Because many of the modifications and extensions in the. NET Framework 3.5 touch on existing classes in the. NET Framework 2.0, such as LINQ, but are also guaranteed backwards compatibility, Microsoft's design team has made some enhancements to the current Visual Basic syntax. Like the syntax sugars, they are also compiled at compile time by the compiler.

What does it enhance?

• Extension methods

• Loosely commissioned

• How to use it?

In fact, this group of features is primarily for class library designers.

1. Extension methods

Extension methods are used to extend a method in an existing CLR class library or other class library, which is actually a kind of "syntactic sugar," just bigger and more important. In the. Net Framework 3.5, this extension is used in many places to extend the type in the existing CLR so that it can use LINQ queries. In essence, an extension method is a function called at compile time to point to a specific extension method, thus achieving the purpose of "extension".

The extension method must be Shared (static in C #) and must be located in the Module (static Class in C #).

Sample code

Imports System.Runtime.CompilerServices
Public Module SampleModule Sample
<Extension()> _
Public Function MySum()Function MySum(ByVal [Me] As IEnumerable(Of Integer)) As Integer
 Dim I As Integer
 For Each Obj As Integer In [Me]
  I += Obj
 Next
 Return I
End Function
End Module

Extension methods are only used in the Namespace (otherwise not all messed up), or imported using Imports. The same is true of the Imports System.Linq namespaces that are required to perform LINQ.

2. Relaxation of the Commission

In the previous version of Visual Basic 9, the method bound to the delegate identifier must exactly match the signature of the delegate type. This brings a lot of unnecessary headaches, such as the pain of weak types when it comes to Thread binding methods.

In Visual Basic 9, the binding and definition of a delegate will allow for loose type matching as long as the parameter types in the method can be implicitly converted to the target signature. Here we use a typical example to create a New Thread. According to the Assembly head, the relaxation of the delegate can be more relaxed, the samplesub parameters are removed, can still compile ...

Sample code

'在 Visual Basic 8 中,你不可能将这个 Sub 绑定到 ThreadStart 上,但在 VB9 中,一切都成为可能
Public Sub SampleSub()Sub SampleSub(Args As String)
End Sub
'一切都是那么的简单!
Dim Thread As New Thread(AddressOf SampleSub)
Thread.Start()

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.