LINQ to Objects
LINQ to Objects allows you execute SQL-like queries over your arrays and collections. Executing these queries, you are able to filter your collection for more specific processing. The following example executes a simple query against an array of integers:
Imports System
Imports System.Linq
Public Class EntryPoint
Shared Sub Main()
Dim someNumbers As Integer() = {5, 4, 3, 2, 1, 0}
Dim query = From x In someNumbers _
Where x >= 3 _
Select x
For Each item In query
Console.WriteLine("{0} is >= 3", item)
Next
End Sub
This code filters someNumbers to create query, and query will contain the three integers, which are those greater than or equal to 3. The For Each . . . Next statement will loop through query and produce the following results:
5 is >= 3
4 is >= 3
3 is >= 3
Lambda Expressions (Inline Functions)
Lambda expressions provide you with a way to create inline, anonymous methods. The functions you create do not need to be typed, as they infer their types at run time. Creating a VB method to determine even numbers might look like the following:
Shared Function IsEven(ByVal x As Integer) As Boolean
If x Mod 2 = 0 Then
Return True
Else
Return False
End If
End Function
Using an inline function, you can reduce the amount of code needed to accomplish the same purpose, as in the following code snippet:
Function(n) n Mod 2 = 0
Extension Methods
Extension methods allow you to add methods to an existing CLR type. This enables you to expand the functionality of a type without needing to create a subclass. The following example shows the syntax for an extension method:
<Extension()> _
Public Function IsTickerValid(ByVal aTicker As String) As Boolean
Dim ValidTicker As Boolean = True
If aTicker.Length > 4 Then
ValidTicker = False
End If
Return ValidTicker
End Function
Extension methods avoid the need to create a separate class, with a shared method, to validate the ticker symbol.
Anonymous Types
Using anonymous types, you are able to declare, define, and use types that the VB compiler will generate for you in the background. Anonymous types are able to infer field names and will create properties for these fields as well. This example shows an anonymous type declared in VB:
Dim anAnonymous = New With {.FirstName = "Jodi", .LastName = "Fouche"}
IntelliSense Everywhere
When using extension methods and anonymous types, you will find that the VB IDE is aware of these types and methods. As such, it includes your extension methods with the extended type's methods in all IntelliSense lists while coding. Anonymous types will show their compilergenerated properties in IntelliSense lists while coding.
Nullable Types, Enhanced in VB 2008
Nullable type syntax is enhanced in VB 2008. A new type modifier (?) allows you to define your types as nullable. The following code snippet demonstrates the current syntax and the new shortcut.
Dim x As Nullable(Of Integer)
Dim y As Nullable(Of Integer)
Dim x As Integer?
Dim y As Integer?
Relaxed Delegates, Enhanced in VB 2008
Relaxed delegates extend and enhance the VB's implicit conversions to delegate types. Using relaxed delegates allows you to omit arguments from event handlers. For example, the following code statements are now valid in VB:
Sub OnClick(ByVal sender As Object, ByVal e As Object) Handles aButton.Click
Sub OnClick Handles aButton.Click
Option Infer
Using Option Infer lets you specify whether VB should enforce type declarations for your types. While setting Option Infer On may allow for quicker development, it may lead to maintenance challenges down the road.
Option Infer On is the default setting for new projects in VB 2008 and can be changed via Project Properties >> Compile.