Beruflich Dokumente
Kultur Dokumente
Contents
Contents..................................................................................................................... 1
Introduction................................................................................................................ 2
New Language Features.............................................................................................2
Multi-Line Statement Lambdas................................................................................2
Sub Lambdas........................................................................................................3
Single-line Sub Lambdas......................................................................................3
Uses of statement lambdas..................................................................................4
Implicit Line Continuation........................................................................................4
Auto-implemented Properties..................................................................................7
Collection Initializers...............................................................................................7
Array Literals...........................................................................................................8
Optional Parameters Default To Nothing.................................................................9
Generic Co- and Contra-variance............................................................................9
Limitations..........................................................................................................11
Interop with Dynamic Languages..........................................................................11
Compiling without PIAs..........................................................................................12
Resources.................................................................................................................13
[1]
Introduction
We are proud to release Visual Basic 10, the latest version of the Visual Basic
language. This is the version of the language that ships with Visual Studio 2010
and .NET 4.
Microsoft has committed to a policy of “co-evolution” between Visual Basic and C#,
where major new features will appear in both languages at the same time, and
important new features that appear in both languages in this release include interop
with the Dynamic Language Runtime, and the ability to build solutions that no
longer require Primary Interop Assemblies to be deployed on end-user machines.
Some new features in this version of the language, such as implicit line
continuations, provide a cleaner or simpler syntax that will be used in almost every
function you write. Other new features, such as statement lambdas, enable
powerful idioms in new Microsoft frameworks such as Silverlight 4 and the Task
Parallel Library.
Visual Basic 10 also marks the first release in which the language specification is
deployed with the product. It can be found in C:\Program Files (x86)\Microsoft Visual
Studio 10.0\VB\Specifications.
Visual Basic has always strived to be the most productive tool for building line-of-
business, data-centric applications. Version 9.0 of the language delivered Language
Integrated Query (LINQ), first-class XML support, as well as other core features such
as nullable types. We are proud to release Visual Basic 10, which makes the
language cleaner and simpler and more powerful.
End Function)
[2]
Just like regular lambdas, the compiler will infer the type of each parameter where
possible (in this case inferring n as Integer). The compiler will also infer the
lambda’s return type by using the dominant type algorithm (see section 8.13 of the
Language Specification, usually at “C:\Program Files (x86)\Microsoft Visual Studio
10.0\VB\Specifications”) to choose between the types of each expression in a
Return statement. For example:
If n Mod 2 = 0 Then
Return 3
Else
Return 3.14
End If
End Function
In this case, the compiler sees that the return type could be Integer or Double, and
so it picks Double as it is a wider type that can encompass any Integer.
Alternatively, you can explicitly specify the return type you want by using an As
clause. In this case, the compiler will just use “Single” and will not attempt to infer
a return type:
Sub Lambdas
Just as the “Function” keyword can be used to create a multiline lambda that
returns a value, the “Sub” keyword can be used to create a multiline lambda that
does not return a value:
Array.ForEach(nums, Sub(n)
Console.Write("Number: ")
Console.WriteLine(n)
End Sub)
With Visual Basic 10, lambdas can now contain a single statement:
[3]
'Valid
Array.ForEach(nums, Sub(n) Console.WriteLine(n)) 'Valid
client.DownloadStringAsync(New Uri("http://blogs.msdn.com/lucian"))
Note: to create Silverlight projects you have to download the Silverlight 4 SDK. Also,
read on that site why Silverlight’s protection against cross-site scripting attacks will
cause it to throw a SecurityException, and how to use a file “clientaccesspolicy.xml”
to make it work.
Another important use of statement lambdas is in the new Task Parallel Library. For
instance,
At a high level, the line continuation character is implicit in the following situations:
1. After an attribute
2. After a comma
[4]
6. After a (, {, or <%=
7. Before a ), }, or %>
The following code demonstrates some of the places where implicit line
continuation can be used:
<Attribute()>
Function Go(
ByVal x As Integer,
ByVal y As Integer,
ByVal z As Integer
)
Dim query =
From n In {
123,
456,
789
}
Order By n
Select n +
x
End Function
[5]
Order by X X
Select (in query context) X X
Skip X X
Skip While X X
Take X X
Take While X X
Where X X
In X X
Into X X
On X X
Ascending X X
Descending X X
^ Operator X
* Operator X
/ Operator X
\ Operator X
Mod Operator X
+ Operator (unary and X
binary)
^= Operator X
*= Operator X
/= Operator X
\= Operator X
+= Operator X
-=Operator X
<<= Operator X
>>= Operator X
&= Operator X
< Operator X
<= Operator X
> Operator X
>= Operator X
= Operator X
<> Operator X
Is Operator X
IsNot Operator X
Like Operator X
& Operator X
And Operator X
Or Operator X
Xor Operator X
[6]
AndAlso Operator X
OrElse Operator X
<< Operator X
>> Operator X
Auto-implemented Properties
Developers often need to create simple entity classes or containers for data, in
which the properties defined follow a very simple structure:
In this case the compiler will generate a backing field with the same name as the
property, but with a preceding underscore. It will also fill in the property’s getter
and setter.
<DefaultValue("-")>
Property Name() As String Implements ICustomer.Name
Collection Initializers
Collection initializers provide a way to initialize a collection type and provide it with
a series of default values within a single expression. For example:
[7]
Dim list = New List(Of String) From {"abc", "def", "ghi"}
Each element after the “From” keyword will be passed to the Add method on the
type:
list.Add("abc")
list.Add("def")
list.Add("ghi")
Class Customer
Property ID As Integer
Property FirstName As String
Property LastName As String
End Class
<Extension()>
Sub Add(list As List(Of Customer),
ID As Integer,
FirstName As String,
LastName As String)
Array Literals
Array literals provide a compact syntax for declaring an array whose type is inferred
by the compiler.
[8]
Dim d = {1, "123"} 'infers Object() (warning with Option Strict On)
An array literal always takes its type from the context, if applicable:
Only when the context doesn’t say which type to use, will the type of the array be
inferred from its elements.
An optional parameter can be initialized either with a constant (if the type of the
parameter is compatible with that constant), or with the “Nothing”. Note that
“Nothing” is the only expression that can be used to initialize a user-defined class or
structure.
Intuitively it makes sense, on the grounds that “if someone asks to enumerate
through a collection of objects, and I give them my collection of strings, it should be
fine since String inherits from Object.” This is called covariance.
[9]
Dim f As Action(Of Object) = Function(o As Object) Console.WriteLine(o)
Dim g As Action(Of String) = f
The intuition here is that “if this action has the power to print out any object, and
someone asks just for an action which has the power to print any string, it should be
fine since String inherits from Object”. This is called contravariance.
What has changed to allow these examples is that some key interfaces and
delegates in the .Net framework are now declared with the “Out” and “In”
keywords:
The “Out” keyword here means that IEnumerable(Of Type1) can be widened to
IEnumerable(Of Type2) so long as Type1 has a widening reference conversion to
Type2. For instance, String has a widening reference conversion to Object, and
ArgumentException has a widening reference conversion to Exception.
The “In” keyword works in reverse. Here it means that Action(Of Type1) can be
widened to Action(Of Type2) so long as Type2 has a widening reference conversion
to Type1.
The keywords “Out” and “In” are only allowed in interface and delegate
declarations. Here is an example which combines both:
For reasons of type safety, the “Out” generic type parameters may only ever be
used in output positions, e.g. as the return type of a function or as the type of a
readonly property. And the “In” generic type parameters may only ever be used in
input positions, e.g. as the type of an argument or the type of a writeonly property.
[10]
Language Specification (installed at C:\Program Files (x86)\Microsoft Visual Studio
10.0\VB\Specifications).
Limitations
Variant type parameters can only be declared on interfaces and delegate types, due
to a restriction in the CLR. Variance only applies when there is a reference
conversion between the type arguments. For instance, an IEnumerable(Of Integer)
is not an IEnumerable(Of Object), because the conversion from Integer to Object is
a boxing conversion, not a reference conversion.
When writing your own frameworks and classes, our guidance on co- and contra-
variance is to use it only if doing so doesn’t involve creating extra interfaces. For
instance, .Net4 introduces a readonly class called “Class Tuple(Of T1,T2)”. It is
readonly, and so only uses T1 and T2 in output positions, and so they could have
been marked with the Out keyword. But Tuple is a class: the only way to allow
covariance would be through an extra “Interface ITuple(Of Out T1, Out T2)” which
Class Tuple then inherits from. We decided not to do this: it is not worth introducing
extra interfaces for the sole purpose of co- or contra-variance.
Over the past few years, there has been renewed developer interest in dynamic
languages (such as Python/Ruby) and their accompanying libraries/frameworks.
These languages and frameworks can use the DLR’s IDynamicObject interface to
define the meanings of dynamic operations, or in the case of an API to allow direct
access to an object’s properties through property syntax (a good example of this is
the HTML DOM in Silverlight).
Latebinding continues to work the same way it always has in Visual Basic, but the
latebinder itself has been updated to recognize when an object implements the
IDynamicObject interface. This allows Visual Basic developers to be able to fully
interoperate with dynamic languages such as IronPython and IronRuby, as well as
APIs that implement IDynamicObject
For example, the following code calls a method defined in the Python library
“random.py”:
[11]
And this example uses one of the new DLR classes:
In order to turn on this feature, select the reference in Solution Explorer and then
set “Embed Interop Tyeps” to “True” in the Properties window:
[12]
Resources
For more information, visit the Visual Basic Team Blog
(http://blogs.msdn.com/vbteam) and the Visual Basic Developer Center
(http://msdn.com/vbasic). Have fun with Visual Basic 10!
[13]