LINQ Tutorial

Intertech Tutorials

LINQ Tutorial Objective

With the release of .NET 3.5, the C# and VB programming languages have been upgraded with a number of new syntactic constructs. Here you will examine implicit data typing, extension methods, object initializers, anonymous types, and lambda expressions.

Although these new language features can be used in any type of .NET 3.5 application, these new constructs are especially useful when programming with LINQ technologies, which the next chapter introduces.

Introducing the Core .NET 3.5 Language Changes

Most releases of the .NET platform include changes to the C# and VB programming languages. .NET 3.0 did not alter the C# or VB languages but introduced three new APIs (WPF, WCF, and WF). .NET 3.5 not only introduces new APIs but many new syntactical constructs for C# and VB. .NET 3.5 brings a slew of new language features, far more than any prior release of the Framework.

In this chapter, you will examine the following new programming constructs:

.NET 3.5 also updates C# 3.0 and VB 9.0 with some additional minor but useful programming constructs. C# now supports partial methods and automatic properties. VB supports partial methods and relaxed delegates. Consult the .NET Framework 3.5 SDK documentation for details on these updates.

The new .NET 3.5 programming constructs can be used within any .NET application. As you will see, however, the major benefit of these new features is that they enable the LINQ programming model. To be sure, when programming with LINQ, you will need to make use of the features shown here.

Introducing Implicitly Typed Local Variables

C# 3.0 and VB 9.0 now support implicit typing of local variables. This is in contrast to explicit typing of local variables, which is achieved using a very predictable syntax based on your language of choice.

Using explicit typing, the developer knows ahead of time the ‘type of type’ for a local variable (System. Int32, System.Boolean, System.String, and so on). Consider the following use of explicit typing:

// C#
static void ExplicitLocalVars()
// Explicitly typed local variables are declared in C# as follows:
// dataType variableName = initialValue;
int myInt = 0;
bool myBool = true;
string myString = "Time, marches on...";

' VB
Sub ExplicitLocalVars()
' Explicitly typed local variables are declared in VB as follows:
' Dim variableName As dataType = initialValue
Dim myInt As Integer = 0
Dim myBool As Boolean = True
Dim myString As String = "Time, marches on..."
End Sub

The underlying type of an implicitly typed local variable is determined at compile time. The underlying type is based on the initial assignment of the implicit variable. This can be verified via reflection services. Consider the following C# code (the corresponding VB code would be similar):

// C# (VB code would be similar)
static void ImplicitLocalVars()
Console.WriteLine("***** Fun with Implicit Typing *****\n");
// Explicitly typed local variables are declared in C# as follows:
var myInt = 0;
var myBool = true;
var myString = "Time, marches on...";
// Print out the underlying type via reflection.
Console.WriteLine("myInt is a: {0}", myInt.GetType().Name);
Console.WriteLine("myBool is a: {0}", myBool.GetType().Name);
Console.WriteLine("myString is a: {0}", myString.GetType().Name);


Copyright (c) 2008-2013. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.