Jaa


Being more productive with C# 3.0

The C# language has evolved a lot in the last few years.

With generics, iterators and anonymous methods in C# 2.0, the groundwork was laid for what would eventually become LINQ. Now with C# 3.0 features such as query expressions, lambda expressions and extension methods, LINQ is finally coming to life.

C# 3.0 is about more than just LINQ, however. It's also about the code you write every day, and the little things that make your life easier as a developer. Here are three new C# 3.0 features that let you save time and code, even when you're not writing queries. These features are available in VS 2008 Beta2 if you want to start playing around with them.

Automatic Properties - Many people use public fields just to avoid typing out all that is needed to declare publicly accessible properties. With a new feature called automatic properties, we have made this simple and have removed a lot of repetitive infrastructure from your code. Your code is now as short and simple as public fields. Because they are real properties, you can make implementation changes later without breaking any existing code. Hopefully now you find it easier to resist the temptation to use public fields instead of properties. Check out some examples of how to use these here.

Extension Methods - Sometimes you want to take a private helper method and reuse it elsewhere. Your first thought might be to add the method to one of your own classes as an instance method, but the method may not really operate on any of your own objects. Your only other option would be to make a helper class and add a static method to it. In C# 3.0 you can use the “this” keyword to make an extension method on a common type such as String and your method will appear where it logically belongs. Examples are available here

Type Inference - The var keyword, which is necessary when using anonymous types, can also reduce redundancy in your other variable declarations. Since the new clause in a declaration makes it obvious what type is being created, you can simply use the var keyword to declare the type instead of typing out a long, repetitive declaration as you often get with generic classes. More details can be found here.

Namaste!

Comments

  • Anonymous
    September 21, 2007
    PingBack from http://msdnrss.thecoderblogs.com/2007/09/21/being-more-productive-with-c-30/

  • Anonymous
    September 21, 2007
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    September 23, 2007
    [原文地址] Halo-themed version of Tafiti [原文发表时间] Friday, September 21, 2007 4:59 PM 今天我们发布了 Tafiti 的 特别主题版本来庆祝

  • Anonymous
    September 23, 2007
    A lot of these "improvements" seemed to me to be fairly spurious at first, and given the choice, I would gladly keep the language simple and do without them.  But it turns out that most/all of them are need for LINQ.  Eric White has a fantastic Functional Programming tutorial where he brings all the new features together and shows how all need to be in place in order to support LINQ.  This tutorial helped me a lot to put all of the new C# features into perspective. http://blogs.msdn.com/ericwhite/pages/FP-Tutorial.aspx

  • Anonymous
    October 29, 2007
    I found your blog quite a nice homely place to keep track of the latest from Microsoft - very informative and yet pleasant reading... thank you sir, will stay tuned.

  • Anonymous
    November 01, 2007
    Here's a roundup of some links to great blogs and articles about the new stuff coming soon in "Orcas".

  • Anonymous
    November 02, 2007
    Here's a roundup of some links to great blogs and articles about the new stuff coming soon in "Orcas"

  • Anonymous
    November 18, 2007
    As of this morning, the RTM version of Visual Studio 2008 (was codenamed Orcas) is available for download

  • Anonymous
    November 18, 2007
    As of this morning, the RTM version of Visual Studio 2008 (was codenamed Orcas) is available for download