
Introducing the Core .NET 3.5 Language Changes
• Each release of the .NET platform included some changes to the C# and VB programming languages.
• .NET 3.5 is no exception.
• In fact, .NET 3.5 brings a slew of new language features, far more than any prior release of the Framework.
• In this chapter, we will examine the most dramatic language changes which are already baked into .NET
3.5 Beta 1:
• Implicit typing of local variables.
• Extension methods.
• Object initialization syntax.
• Anonymous types.
• Lambda expressions.
• While all of these new programming constructs can be used directly ‘out of the box’, their true usefulness
will be seen when programming with LINQ technologies.
• LINQ is a functional programming model which allows you to iterate over various data stores using
strongly typed SQL-like statements.
• We will examine LINQ in the next chapter.
• Be aware that as of .NET 3.5 Beta 1, VB does not yet have compiler support for lambda expressions.
• We should find such support with Beta 2, via the Function statement.
• The final release of .NET 3.5 will also update C# 3.0 and VB 9.0 with some additional minor updates. For
example:
• Both languages should have support for ‘partial methods’.
• VB will now have a nullable type operator (like the ? and ?: operators found in C#).
• VB will now have support for relaxed delegates (a.k.a., covariance / contravariance).
Implicitly Typed Local Variables
• C# 3.0 / 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, etc).
// C#
static void ExplicitLocalVars()
{
// Explicitly typed local variables are declared as so:
// dataType variableName = initialValue;
int myInt = 0;
bool myBool = true;
string myString = "Time, marches on...";
}
' VB
Sub ExplicitLocalVars()
' Explicitly typed local variables are declared as so:
' Dim variableName As dataType = initialValue
Dim myInt As Integer = 0
Dim myBool As Boolean = True
Dim myString As String = "Time, marches on..."
End Sub
• Implicit typing of local variables allows the developer to omit the ‘type of type’ at the time a variable is
declared.
• In C# var keyword is used for implicit typing of data.
• VB programmers simply omit the data type itself (e.g., no As clause).
// C#
static void ImplicitLocalVars()
{
// Implicitly typed local variables.
var myInt = 0;
var myBool = true;
var myString = "Time, marches on...";
}
' VB
Sub ImplicitLocalVars()
' Implicitly typed local variables.
Dim myInt = 0
Dim myBool = True
Dim myString = "Time, marches on..."
End Sub
• The underlying type of an implicitly typed local variable is determined at compile time.
• This can be verified via reflection services.
• Consider the following C# code (VB code would be similar)
// C#
static void ImplicitLocalVars()
{
Console.WriteLine("***** Fun with Implicit Typing *****\n");
// Implicitly typed local variables.
var myInt = 0;
var myBool = true;
var myString = "Time, marches on...";
// Print out the underlying type.
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);
}
• In C#, implicit typing can only be done for local variables.
• You cannot implicitly type member variables, return values or method parameters.
// C#
class ThisWillNeverCompile
{
// Error! var cannot be used on field data!
private var myInt = 10;
// Error! var cannot be used as return values
// or parameter types!
public var MyMethod(var x, var y){}
}
• In VB, it is legal to use implicit typing of non-local data points, but only when Option Strict is Off (the
default behavior).
• Therefore, the following VB code will not compile, as Option Strict is On.
' VB
Option Strict On
' These errors will not be found if Option Strict is Off.
Class ThisWillNeverCompile
' Error! var cannot be used on field data!
Private myInt = 10
' Error! var cannot be used as return values
' or parameter types!
Public Sub MyMethod(ByVal x, ByVal y)
End Sub
End Class
• Implicit typing can also be used within a ‘for each’ construct.
• The compiler will ensure that the container is compatible with the iteration variable.
// C#
static void ImplicitVarInForEach()
{
// Implicit array of System.Int32 types.
var evenNumbers = new int[] { 2, 4, 6, 8 };
// Here, item is a System.Int32.
foreach (var item in evenNumbers)
{
Console.WriteLine("Item value: {0}", item);
}
}
' VB
Sub ImplicitVarInForEach()
' Implicit array of System.Int32 types.
Dim myIntArray = New Integer() { 2, 4, 6, 8 }
' Here, item is a System.Int32.
For Each item In myIntArray
Console.WriteLine("Item value: {0}", item)
Next
End Sub
• Like a constant variable, the value assigned to an implicitly typed local variable must be done at the point
of declaration.
• In C#, implicitly typed local variables cannot be assigned the value of null at the time of declaration (but
can be assigned to null after the fact).
• It is permissible for the assigned value to be a literal value or the value of another variable.
// C#
static void ImplicitDataTests()
{
// Error! Must assign a value!
var myData;
// Error! Must assign value at time of declaration!
var myInt;
myInt = 0;
// Error! Can’t assign null at declaration!
var myObj = null;
// OK!
var myCar = new SportsCar();
myCar = null;
// Also OK!
var myInt2 = 0;
var anotherInt = myInt2;
// Also OK!
string myString = "Wake up!";
var myData2 = myString;
}
• VB has similar restrictions, assuming Option Strict is Off.
• Be aware that VB now supports Option Infer (On or Off).
• This new option can be used to disable inference of local variables at a project or file level.
' VB
Option Infer Off
Option Strict On
Module Module1
' These are all errors with the current options!
Sub ImplicitLocalVars()
Dim myInt = 0
Dim myBool = True
Dim myString = "Time, marches on..."
End Sub
End Module
• Unlike a COM VARIANT or loosely typed scripting languages, implicitly typed data is indeed strongly
typed.
• The C# compiler will not allow you to assign incompatible values after the initial assignment.
• The same holds true in VB if Option Strict is enabled.
// C#
// The following code is fine, as the compiler knows ‘s’ is a string.
var s = "This variable can only hold string data!";
s = "This is fine.";
// Error! ‘s’ is a string, not an integer!
s = 44;
• C# 3.0 also allows you to define implicitly typed local arrays.
• The current release of VB 9.0 does not support this syntax.
• In C# 3.0, you do not need to explicitly define the underlying array type.
• However, like any array, you cannot mix values when initializing the array.
• You can however have an array with values and default values for a given data type.
// C#
static void ImplicitArrays()
{
// a is really int[].
var a = new[] { 1, 2, 3, 4000 };
// b is really double[].
var b = new[] { 1, 1.5, 2, 2.5 };
// c is really string[].
var c = new[] { "we", null, "are", null, "family" };
// myCars is really SportsCar[].
var myCars = new[] { new SportsCar(), new SportsCar()};
// Error! Mixed types!
var d = new[] { 1, "one", 2, "two" };
}
• DO NOT use implicitly typed local variables as a ‘simple time saver’.
• When you know you need an integer, declare an integer.
• Overuse of implicit typing can make your code confusing to others.
• When you are working with LINQ query expressions, you will find implicit typing to be extremely
helpful.
• The underlying data type returned from a LINQ query expression is seldom obvious.
• In these cases, implicit typing can be used to hold the result of a LINQ query expression in a strongly
typed (and simplified) manner.
Copyright (c) 2008. 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.
.NET 3.5 Language Changes
Table of Contents
Courseware
Training Resources
Tutorials