I'm really loving new C# 3.0 features

The more I use new C# 3.0 syn­tax the more I love it. I basi­cally still use .NET 2.0/3.0 and VS 2005 at work, but at home I'm migrat­ing to Orcas. And I have to admit that more and more, when I'm at work I wish I could use new syntax.

  • var key­word: it's as sim­ple 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. Ini­tially I thought that using this new key­word would make code less read­able. That I would have to mouse over a vari­able name in Visual Stu­dio 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 prop­erly makes code actu­ally eas­ier to read, faster to write, and often you don't really care about what's the type of that vari­able, espe­cially 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.

  • lamb­das: I love anony­mous del­e­gates, they are great but they can be a pain in the butt to type. Take the fol­low­ing 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 del­e­gate and long type names, it takes a while to find out what's really going on there. And it's much more typ­ing 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 con­cise, but eas­ier to read as well. All those del­e­gate key­words, and curly braces (espe­cially with nested anony­mous del­e­gates like in the exam­ple) cre­ate noise that you have to read your way through. With lamb­das it's much cleaner. So far lamb­das are my favorite new addi­tion to the language.

  • auto­matic prop­er­ties: Is this a class or is this an inter­face? That's actu­ally one of few cases where I pre­fer to be more elab­o­rate about things, and still it's eas­ier to accom­plish (and less typ­ing) with ReSharper than with those odd interface-like syntax.
  • Collections/object ini­tial­iz­ers: Another fea­ture that is more of a syn­tac­tic sugar than real value, but I like it, espe­cially in tests I often want to setup a col­lec­tion to have mul­ti­ple ele­ments, and this way feels really more nat­ural, than call­ing Add sev­eral times, or cre­at­ing an array just to call AddRange. I have one con for object ini­tial­iz­ers. They are great too, as a mater of fact they are so great that it may be tempt­ing to use them all over the place, and thus cre­at­ing muta­ble object, in places where immutable object would be more appro­pri­ate. With them it's eas­ier 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 remain­ing fea­tures next time.

Comments are closed.