# Saturday, April 21, 2007
« .NET methods should default to virtual | Main | LINQ query expressions »
I finally downloaded and installed Visual Studio "Orcas" beta 1 so that I could start playing around with the new functionality built-in to .NET 3.5.  I wanted to build a sample that compared an implementation between .NET 1.1, .NET 2.0, and .NET 3.5, here's the contrived example that I came up with based on a series of postings by Scott Guthrie.

Each example is making use of a simple POCO, note that in .NET 3.5 the private variables and public properties could be replaced by automatic properties.

public class Person
{
    private string firstName;
    private string lastName;
    private int age;

    public Person() { }

    public Person(string firstName, string lastName, int age)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public override string ToString()
    {
        return String.Format("{0} {1} - {2}", FirstName, LastName, Age);
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }
}


The .NET 1.1 implementation is pretty terse, and requires the most work on the part of the developer.  Although I think it reads horribly, pretty much any developer (even Mort) can understand exactly what the code is doing.

public static void DotNet1()
{
    ArrayList people = new ArrayList();
    people.Add(new Person("Shawn", "Neal", 28));
    people.Add(new Person("Isabella", "Neal", 16));
    people.Add(new Person("John", "Smoe", 43));

    ArrayList teens = new ArrayList();
    foreach (Person person in people)
    {
        if (person.Age < 20 && person.Age > 12)
            teens.Add(person);
    }

    Console.WriteLine("The following people are teenagers:");
    foreach (Person o in teens)
        Console.WriteLine(o.ToString());
}

In .NET 2.0 things become easier for the developer with anonymous delegates and the new generic List implementation.  The only problem is that the syntax doesn't flow well and Mort might not get it, but at least the nasty foreach statement is gone.

public static void DotNet2()
{
    List<Person> people = new List<Person>();
    people.Add(new Person("Shawn", "Neal", 28));
    people.Add(new Person("Isabella", "Neal", 16));
    people.Add(new Person("John", "Smoe", 43));

    List<Person> teens = people.FindAll(delegate(Person p) { return p.Age < 20 && p.Age > 12; });
    Console.WriteLine("The following people are teenagers:");
    foreach (Person o in teens)
        Console.WriteLine(o.ToString());
}

Things in .NET 3.5 become a whole lot cleaner and elegant.  The people.Add() statements are replaced by much cleaner object initializers.  I also used the object initializer syntax for each person instance, even though I provided an overloaded ctor; in .NET 3.5 I could actually forgoe the overloaded ctor.  The most interesting part is that the List.FindAll has been replaced by a LINQ Where extension method with a lambda expression argument - compare that to the .NET 1.1 implemetation!  The final bit is that I added my own WriteAll() extension method just for fun.

public static void DotNet35()
{
    IList<Person> people = new List<Person>()
    {
        new Person() { Age = 28, FirstName = "Shawn", LastName = "Neal" },
        new Person() { Age = 16, FirstName = "Isabella", LastName = "Neal" },
        new Person() { Age = 43, FirstName = "John", LastName = "Smoe" }
    };

    IEnumerable<Person> teens = people.Where<Person>(p => p.Age < 20 && p.Age > 12);
    Console.WriteLine("The following people are teenagers:");
    teens.WriteAll();
}

public static class MyExtension
{
    public static void WriteAll(this IEnumerable enumerable)
    {
        foreach (object o in enumerable)
            Console.WriteLine(o.ToString());
    }
}

I'm now thinking of using .NET 3.5 on my current pet project - LINQ and extension methods are absolutely fantastic!


Comments are closed.