I’m really loving new C# 3.0 features

The more I use new C# 3.0 syntax the more I love it. I basically still use .NET 2.0/3.0 and VS 2005 at work, but at home I’m migrating to Orcas. And I have to admit that more and more, when I’m at work I wish I could use new syntax.

  • var keyword: it’s as simple as it can be, but when you get used to writing:
var filteredItem = filter(item);

instead of:

FilteredItem<MtfModificationLog> filteredItem = filter(item);

it’s really hard to go back. Initially I thought that using this new keyword would make code less readable. That I would have to mouse over a variable name in Visual Studio all the time to see it’s type, but after I’ve used it some time, I changed my mind by 180 degree.It all depends on how you use it, but I’ve found, that using it properly makes code actually easier to read, faster to write, and often you don’t really care about what’s the type of that variable, especially with generic types. What you care about is that it’s FilteredItem<T>, and the  typeof(T) is at this point of code not that important.

  • lambdas: I love anonymous delegates, they are great but they can be a pain in the butt to type. Take the following code:
MtfConcept c2 = _concept.Filter(

    delegate(MtfModificationInfo i)

        {

            return new FilteredItem<MtfModificationInfo>

                (delegate(MtfModificationInfo i2)

                     {

                         return DateTime.Now.Subtract(i2.Date).Days < 1 &&

                                i2.UserName == "Pies";

                     }, i);

        });

It doesn’t do much, but with this all delegate and long type names, it takes a while to find out what’s really going on there. And it’s much more typing than this:

MtfConcept c2 = _concept.Filter(

    i => new FilteredItem<MtfModificationInfo>(

        i2 => DateTime.Now.Subtract(i2.Date).Days < 1 && i2.UserName == "Pies", i));

I find it not only more concise, but easier to read as well. All those delegate keywords, and curly braces (especially with nested anonymous delegates like in the example) create noise that you have to read your way through. With lambdas it’s much cleaner. So far lambdas are my favorite new addition to the language.

  • automatic properties: Is this a class or is this an interface? That’s actually one of few cases where I prefer to be more elaborate about things, and still it’s easier to accomplish (and less typing) with ReSharper than with those odd interface-like syntax.
  • Collections/object initializers: Another feature that is more of a syntactic sugar than real value, but I like it, especially in tests I often want to setup a collection to have multiple elements, and this way feels really more natural, than calling Add several times, or creating an array just to call AddRange. I have one con for object initializers. They are great too, as a mater of fact they are so great that it may be tempting to use them all over the place, and thus creating mutable object, in places where immutable object would be more appropriate. With them it’s easier to do the wrong thing.
    [SetUp]

    public void SetUpDates()

    {

        _dates = new List<DateTime>()

        {

            new DateTime(2007,12,4),

            new DateTime(2007,11,4),

            new DateTime(2007,10,4),

            new DateTime(2007,9,4),

            new DateTime(2007,8,4),

            new DateTime(2007,7,4),

            new DateTime(2007,6,4)

        };

    }

I’ll write about remaining features next time.