Skip to main content

C# 3.0: Relational Language Operatings, Type Inference, and More

Posted by javaben on September 13, 2005 at 2:01 PM PDT

I'm here at Microsoft's Professional Developer Conference (PDC), the Big Redmond's irregularly scheduled conference where they introduce new technology to developers. It's a pretty surreal experience. I'm using the only Mac as far as the eye can see. It's kind of like JavaOne in a parallel universe.

This morning, Bill Gates and Jim Allchin got up and talked about a lot of the old stuff we've been hearing about for years now: Avalon/WPF for graphics, Indigo/WCF for services, etc. For those not following Microsoft, these are really cool technologies, but... old news.

The real interesting action happened when Anders Hejlsberg and Don Box got up and demonstrated Microsoft's LINQ project. It turns out that in a future release of .NET (3.0), Microsoft will embed relational operators into the language itself. To understand what this means, check out this C# code:

public void Linq1() {
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    var lowNums =
        from n in numbers
        where n < 5
        select n;

    Console.WriteLine("Numbers < 5:");
    foreach (var x in lowNums) {
        Console.WriteLine(x);
    }
}

Err, that's interesting. But hang on, it turns out you can mix and match this code with data from relational databases, all using the same operators. Check this out:

public void Linq1() {
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    DataContext context = ...;  // don't worry about how I get a ref to this
    Table<myobject> myTable = context.GetTable<myobject>();

    var lowMyObjects =
        from n in numbers, o in myTable
        where n < 5 and o.MyNumber == n
        select o;

    Console.WriteLine("MyObjects whose number is less than five:");
    foreach (var o in lowMyObjects) {
        Console.WriteLine(o.MyName);
    }
}

Wow. I can seamlessly combine my relational data with my object data. This is just scratching the surface of LINQ (for example, XML is also a first-class type of C# 3); check it out. They've given out pre-release bits; I'll definitely be playing with this stuff.

Oh, did you happen to notice this code snippet in one of the earlier examples?

var lowNums = ...

This is not VB code. C# 3.0 introduces type inference. That's also pretty interesting. Some folks in have been asking why Java doesn't do this for years. That is, if an IDE can figure out how to automatically write the (Cast) operator, why can't the compiler?

I remember asking one of the compiler guys at JavaOne about why they don't introduce type inference in future versions of Java; his answer: "Sounds like you want to use a dynamic language." I'm sure glad some folks understand that you don't need to throw away strong typing to avoid writing the type of a variable over and over again, unnecessarily.

Check out some of the other new features, announced today and coming to some future release sometime in the distant future.

To be clear: this is not a "Run from Java and embrace .NET" post. Rather, I'm excited to see the Java space innovate to keep up with some of these and other really intriguing new C# features. It's fun to watch the game of Java/.NET leap frog play out...

(Check out www.ajaxian.com later today for a blog on some of the Atlas/Ajax stuff that Microsoft announced today...)

(Once again, cross-posted to Married... with Children)

Related Topics >>