Preview of C# version 3.0 features
Wow, C# 2.0 is not officially out of the door, C# 3.0 is already rearing its head :).
In this post I'll go through some of the new features of the 3.0 version of C#.
Feature 1: Implicitly typed local variables.
Variable declaration has become simpler...
var employee = new Employee();
versus
Employee employee = new Employee().
Just a side note, this declaration should be familiar to those who also code in JavaScript / ECMAScript.
Feature 2: Extension methods.
This feature is similar to JavaScript prototype property. The main function is to provide a method for a concrete class which could not be otherwise (due to no source code, etc.), thus extending the class with new method(s).
In JavaScript you could do something like...
String.prototype.trim = function() { return this.replace(...); } and do the following with this new trim function:
var aString = new String(" Hello, World! ");
alert(aString.trim()); //show "Hello, World!" instead of " Hello, World! "
in C# 3.0, extension method can be implemented like so...
namespace SomethingOrOther
{
public static class Extensions
{
public static ToInt32(this string s)
{
return Int32.Parse(s);
}
}
}
and here is how you use it:
using SomethingOrOther;
:
Console.WriteLine("90210".ToInt32());
:
Note that String class do not have a ToInt32 method, but we are extending it using our extension method so it appears that it has a ToInt32() method. The key is declaring static method inside a static class and prefixing the parameter that refers to the object to be extended with
this keyword.
IMHO, JavaScript extension method implementation is a bit cleaner. But, this will do :).
Feature 3: Lambda Expression
Heh, anonymous method on steroid :). Hmm... how do you explain this... Bleh, just click on the link that I provided below and read for yourself :) To be honest, I am still trying to get a grasp on this myself when writing this, hehe. And it looks like it can become its own article :).
Feature 4: Anonymous Type
Basically, C# will create a temporary class based on the parameter being passed.
You now can do something like:
var basket = new { Name = "Basket", Price = 10.00 };
and C# will create a temporary "product" type (which does not exists anywhere your code). You can access the variable as usual after the initialization.
Console.WriteLine("Product Name: {0}, Price: {1}", basket.Name, basket.Price);
Note also, that I was using this with Feature 1 (Implicitly typed local variables) var keyword.
Frankly, I can't see yet why would you need something like this, but who knows... maybe this is very useful with LINQ. Microsoft gave an example of creating an anonymous subset of a predefined class for enumeration. Hmm... Can't you just use the predefined class instead?
Here is an example that they gave:
class Contact
{
public string Name, Phone, Address, State;
public Contact(string name, string phone, string address, string state)
{
this.Name = name;
this.Phone = phone;
//etc..
}
}
var contacts = new List {
new Contact("Michael", "520-331-2718", "33140 SW Liverpool Lane", "WA"),
new Contact("Jennifer", "503-998-1177", "1245 NW Baypony Dr", "OR"),
new Contact("Sean", "515-127-3340", "55217 SW Estate Dr", "WA")
};
foreach(var contact in contacts)
{
// Why would I do this???
var a = new { contact.Name, contact.Phone };
Console.WriteLine("{0} can be reached at {1}", a.Name, a.Phone);
}
// Won't this work just as well??? What's wrong with this?
foreach(var contact in contacts)
{
Console.WriteLine("{0} can be reached at {1}", contact.Name, contact.Phone);
}
Feature 5:Query Expression
This have to do with the LINQ project. For further explanation please look at the site about LINQ project that I linked below.
There are still a lot of other features in C# version 3.0. What I've written here is only a part of it and intended just as a preview.
Of course, all of this is heavily used in LINQ implementation.
To learn more about what's new with C# 3.0, click here.
My suggestion is to download the
On Hand Lab and the
LINQ Technology Preview, install Visual Studio 2005 / Express Beta 2 and start playing with it if you wish to understand the new features better.