C# 3.0 updates

by Miguel de Icaza

Scott Peterson blogs about his recent contributions to the C# 3.0 compiler:

  • Variable type inference (the "var" keyword)
  • Anonymous types.
  • Implicitly typed arrays.
  • Object initialization.
  • Collection initialization.
  • Scott is participating in the Google Summer of Code. His summer project is to port the Banshee Media Player to Windows. But I guess he had to have those 3.0 features before he could start. It makes perfect sense. This patch was reviewed and mentored by Marek Safar who has been implementing various other parts of the C# 3 support (extension methods, the new LINQ parser and the updated delegate semantics).

    The new object initialization allows properties to be set at construction time, for example:

    	// C# 2 style
    	Gtk.Window w = new Gtk.Window ();
    	w.Title = "My App";
    	w.Visible = true;
    	// C# 3 style
    	Gtk.Window w = new Gtk.Window () { Title = "My App", Visible = true };

    This also works for events:

    	Gtk.Button b = new Gtk.Button ("Ok") { Clicked = ok_clicked };

    Or using the new lambda expressions:

    	Gtk.Button b = new Gtk.Button ("Ok") {
    		Visible = true,
    		Clicked = (sender,args) => { Console.WriteLine ("Clicked"); }

    The last case (delegate) is currently not supported by mcs.

    Posted on 09 May 2007