The intersection of technology and leadership

Some notes on C# 3.0

I’ve been playing around with some of the new features of C#3.0. I installed VS2008 and the new Resharper 4.0 EAP available here. I expect most of the Jetbrains EAP stuff to be pretty buggy and I’ve already had it crash on me more than five times today. Still, being able to refactor and navigate around files is worth the buggy trial.

Out of the box support for the new language features looks interesting as well as some of the new tools and refactorings in place. Interestingly, default settings suggests constant use of the new var keyword replacing the class declaration you should be using. I’m a little uncomfortable with this as the default setting though it could be because I’m not used to seeing C# code like that.

I started playing around with extension methods, trying to convert the ReflectUtil I wrote back in this post. After a rewrite, some of the APIs looked a bit better although at the cost of making some private inner classes and delegates public. I’m not sure if I like it yet and still trying to think of some guidelines for when to use it. I think definitely having all the extension methods grouped together would help – either in one big Extension class (if you don’t have too many) or in a [Class]Extension class so they’re much easier to locate. If you find youreself writing StringUtil classes, consider converting them to Extension classes. Right now, extension methods only work on instances, so if you want to be able to add static methods, vote on the topic here. I’m not yet convinced it’s that useful just yet though.

Lambda expressions look especially nice although now it’s just plain confusing how you have three ways to do something and there’s really little benefit out of the two more verbose ways. Use this to replace anonymous delegates with a less verbose method.

3 Comments

  1. Rob Grainger

    I’ve decided IMHO that I’m fond of var, although its usage should probably be moderated – use to improve readability only.

    Basically, I use var when it is clear what the type of the object being assigned is already. It’s particularly good for receiving the results of ‘new’, especially for generic types, especially with multiple parameters.

    var instance = new GenClass;

    is much preferable to…

    GenClass instance = new GenClass;

    The latter seems a clear violation of the DRY principle, and we used to have no choice in the matter!

    In other cases, common sense comes into play. If it’s clear from a method what the return type is, I’m happy to use var. For example..

    foreach (var row in myTable.Rows) { … }

    If it’s unclear, leave the full type in the declaration to improve readability. Of course, if the variable’s type is different from the type being assigned, the full type must be used…

    System.Windows.Form form = new MyForm();

    PS. I wanted to indent the code samples above, but couldn’t see how to do so on the site. If there is any way to place markup in comments, it may be a good idea to indicate so below the entry field.

    Best Regards,

    Rob G

  2. Rob Grainger

    Oops – I just spotted on posting that my GenClass instances lost their parameter lists. They should read GenClass<int, Point, int> in both cases! – i.e.

    GenClass<int, Point, int&gt instance = new GenClass<int, Point, int&gt();

    Regards again,

    Rob G

  3. Rob Grainger

    D’oh – missed a couple of semi-colons, but I hope you get the idea.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 patkua@work

Theme by Anders NorenUp ↑