Extension methods are gonna be fun!

So I've had a (very) small amount of time to play around with Orcas and Silverlight, but some of the C# 3.0 features are going to come in handy. Greg Schechter has already written about using object initializers with XAML objects and back in March Scott Guthrie showed an example of extension methods. I've already come up with a nice little use for the latter.

Because of its declarative nature, there are some properties you can set quite easily inside XAML, but not so easily in C# — specifying the Canvas position for an object is one of those.

<Rectangle Canvas.Left="100" Canvas.Top="50" Width="10" Height="10/>

 Rectangle r = new Rectangle();
r.SetProperty<double>(Canvas.Left, 100);
r.SetProperty<double>(Canvas.Top, 50);

Here's a nice little way object extensions can simplify your life.

 public static class SilverlightExtensions
{
   public static void SetCanvasPosition(this Visual v, double left, double top)
   {
      v.SetValue<double>(Canvas.LeftProperty, left);
      v.SetValue<double>(Canvas.TopProperty, top);
   }
}

Because I need to be able to create a ton of Rectangles programmatically, I can replace the two earlier lines of code with one

r.SetCanvasPosition(100, 50);