views:

810

answers:

5

I was browsing the Hidden Features of C# question and thought I would try out one of the features I was unfamiliar with. Unfortunately I use Visual Studio 2005 and the feature in question was introduced later. Is there a good list for new features in C# 3.0 (Visual Studio 2008) vs. C# 2.0 (Visual Studio 2005)?

+1  A: 

Here's a link to the MS page on .NET 3.0: http://msdn.microsoft.com/en-us/library/bb822048.aspx ...and on VS 2008 for C#: http://msdn.microsoft.com/en-us/library/bb383815.aspx

I haven't tried VS2008 and .NET 3.0 out, but I figure the links might help ;)

ojrac
+4  A: 

This is not a comprehensive list but these are some of my favorite new features of C# 3.0:

New type initializers. Instead of saying this:

Person person = new Person();
person.Name = "John Smith";

I can say this:

Person person = new Person() { Name = "John Smith" };

Similarly, instead of adding items individually, I can initialize types that implement IEnumerable like this:

List<string> list = new List<string> { "foo", "bar" };

The new syntax for lambda expressions is also nice. Instead of typing this:

people.Where(delegate(person) { return person.Age >= 21;);

I can type this:

people.Where(person => person.Age >= 21 );

You can also write extension methods to built in types:

public static class StringUtilities
{
    public static string Pluralize(this word)
    {
       ...
    }
}

Which allows something like this:

string word = "person";
word.Pluralize(); // Returns "people"

And finally. Anonymous types. So you can create anonymous classes on the fly, like this:

var book = new { Title: "...", Cost: "..." };
Matt Ephraim
A: 

One of the unknown but powerful feature of Visual Studio 2008 is T4 (Text Template Transformation Toolkit). T4 is a code generator built right into Visual Studio 2008.

Check the Scott Guthrie's blog post Visual Studio 2008 and .NET 3.5 Released. This post was written when Visual Studio 2008 and .NET 3.5 is Released. This post has included lot of links for the new features of Visual Studio 2008 and C# 3.0.

Shiju
+1  A: 

A couple features I like:

  • VS 2008 supports targeting various version of the .NET framework so you can target 2.0, 3.0 or 3.5

  • Automatic properties are nice.

For example:

public int Id { get; set; }

instead of:

private int _id;
public int Id {
    get { return _id; }
    set { _id = value; }
}
Mike Henry
Arguably, you can get automatic properties by using code snippets in VS 2005 also.
RobS
@RobS : +1 for your comment.
Ram
+1  A: 

Multi-targetting support. You can build .NET 2 -> .NET 3.5 all from the one IDE.

Slace