Thursday, May 1, 2008

C# 3.0 Language Features

I downloaded the C# Version 3.0 Specification early last year and remember being very impressed with the latest round of language features. Implicit local variable types, extension methods, lambda expressions, object and collection initializers! I couldn't wait to sprinkle that syntactic sugar all over my C# code. However, reality had something different in store and it's only been the last few months that I've used C# 3.0 in anger.

If you're just starting out with C# 3.0, I highly recommend paying a visit to Charlie Calvert's blog and watching the videos of Luca Bolognese and Luke Hoban. Luke's webcast is particularly helpful if you're already familiar with the language features in C# 2.0.

I'm not going to delve far into the C# 3.0 language features in this post. However, I have to draw your attention to the introduction of lambda expressions. I've used anonymous delegates with C# 2.0 for a long time and find them invaluable when using generic collections. However, the anonymous delegate syntax is unreasonably verbose and hampers code readability. Lambda expressions offer a very clean syntax for expressing anonymous delegates in C# 3.0. Here's a quick example:


So I guess I'm a little slow on the uptake of lambda expressions but I'm really happy with the way my code is looking now. I know the Visual Studio IDE helps you out a lot with code completion but why type more than you have to.

No comments:

Post a Comment