§ July 29, 2006

The C# 3.0 language specification

I know I'm a bit late here, but better late than never.

I've been reading over some of the changes to C# for version 3. I had heard that v3 would be v2 with the addition of the new WPF, but looking over the new spec (well, fairly new anyway), there is quite a bit more.

Implicit types, method extensions, lambda expressions, object / collection initializers, and LINQ... C# is going to look really weird! So much for simplicity.

Implicit types (csharp meets vb or php) allow you to define the variable without a (or with an implied) type:
var foo = "hello";
var bar = 32;


so foo is implied to be of type string, and bar is implied to be of some numeric type.

Method extensions look very strange. Think of old dog learns new tricks meets operator overloading. Method extensions allow you to create "extension" methods for existing types:
public class Bar {
    string what = "hello";
    public string What { get { return what; } set { what = value; } }
    public Bar() {}
    public void Hello() { 
        Console.WriteLine(what); 
    }
}

public static class Foo {
    public static void SayHelloToSteve(this Bar bar) {
        bar.What += " steve";
        bar.Hello();
    }
    public static void SayHelloToSomeone(this Bar bar, string who) {
        bar.What = who;
        bar.Hello();
    } 
}

public class Program {
    static void Main(string[] args) {
        Bar b = new Bar();
        b.Hello();
        b.SayHelloToSteve();
        b.SayHelloToSomeone("Hello Bob!");
    }
}


Notice that SayHelloToSteve() and SayHelloToSomeone(string s) are not member methods of the Bar class, but are invoked as if they were (by virtue of the Method extension defined in Foo that are available to Bar). This seems like a feature that will be abused to defeat inheritance (by adding reflection to the method extenstions to manipulate internal class memebers without inheriting and changing functionality of the object) that would otherwise look odd and ugly.

OUTPUT:
hello
hello steve
Hello Bob!


At this point lambda expressions dont make much sense to me, but it seems its a way to define more than one method body in an anonymous method.

Object / collection initialization makes initialization a lot easier. I don't know if you've ever wanted to declare a new object inline inside a method, only to realize that you needed to set a few properties before it was used, well this is the answer to that:

myObject.Blah(new SomeObject() { PropertyA = 1, PropertyB = "dude", PropertyC = false });


Then there's LINQ: which feels something like SQL for C#.

feel free to download the spec here


fin
Posted 1 week, 3 days ago on July 29, 2006

 Comments can be posted in the forums.

© 2003 - 2017 NullFX
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License