Skip to Main Content

Wednesday, March 08, 2006

New C# 3.0 Features

While .NET 2.0 has just recently been released, Microsoft has been active in .NET's next release. The C# 3.0 specification was released in September 2005, and their C# 3.0 site has many examples of it's newest features.

Implicitly Type Local Variables

(The var keyword)


One of the new features in 3.0 is the "var" keyword.

Example:
var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var orders = new Dictionary<int, order>();

Once the var variable is assigned, it implicitly converts it to the assigned data type. So in the var i = 5; line, i then becomes an int. Also, in the example of above, the s variable becomes a string, etc.

There are a few restrictions however:
  1. The declarator must include an initializer.

  2. The initializer must be an expression. The initializer cannot be an object or collection initializer by itself, but it can be a new expression that includes an object or collection initializer.

  3. The compile-time type of the initializer expression cannot be the null type.

  4. If the local variable declaration includes multiple declarators, the initializers must all have the same compile-time type.

For example, the following code examples are incorrect:
var x;
var y = {1, 2, 3};
var z = null;

Extension Methods

Extension methods extend existing types by allowing you to add additional methods.

Note: Microsoft recommends that extension methods be used sparingly as they are less discoverable and more limited that instance methods.

Creating the Extension Methods

To declare an extension method, you specify the this keyword in the first parameter of the method.

For example:
namespace MyApp.Utilities
{
public static class Extensions
{
public static int DivideByTwo(this int Number)
{
return Convert.ToInt32(Number/2);
}
}
}

Importing the Extension Methods

To import this extension methods from you Extensions class, use the using keyword to include in the namespace:
using MyApp.Utilities;
Once you include the namespace into the file, you can call the DivideByTwo method from any int variable you create.

For example:
int i = 100;
int halfOfi = i.DivideByTwo(); //Divides i in half


Object Initializers

Yet another added feature in 3.0 is the concept of an object initializer. With object/member initializers, you can set the object's properties while initializing it, thus saving many lines of code.
For example:
public class Car
{
string make, model, color;
int modelYear, doors;

public string Make { get { return make; } set { make = value; } }
public string Model { get { return model; } set { model = value; } }
public string Color { get { return color; } set { color = value; } }
public int ModelYear { get { return modelYear; } set { modelYear = value; } }
public int Doors{ get { return doors; } set { doors = value; } }
}

You could then create a new instance of Car as follows:
var c = new Car {Make = "Ford", Model = "Mustang",
Color = "Red", ModelYear = 1967, Doors = 2};

Not only can you initialize classes this way, you can initialize classes anonymously this way. These are called Anonymous Types. So for instance, you could create a new class like:
var person1 = new { Name = "Bob", Age = 31 };
var person2 = new { Name = "Jane", Age = 37 };

If you wanted to, you could then use:
person1 = person2;
The reason you can do this is because they are the same anonymous type.
There are a few more features in C# 3.0, but I will discuss them more later.

0 Comments:

Post a Comment

<< Home