Craig Nicholson

My blog has moved to http://craign.net/.

News

My blog has moved to http://craign.net/.


AfrigatorI shmaak SA Blogs, sorted with Amatomu.comSign the petition for Xbox LIVE in South Africa. Locations of visitors to this page

Communities

Development Blogs

Development Tools

General Tools

Personal

Silverlight 1.1 UserControl C# template code does not follow best practices

Why does the Microsoft Visual Studio team not follow the best practices internally? When creating a Silverlight 1.1 UserControl under Visual Studio 2008 Beta 2 it generates the following code by default.

    System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("Project1.UserControl1.xaml");
    this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());

Most developers would realise that this is not good code. Why do I say this, well firstly the code is creating two objects, a System.IO.UnmanagedMemoryStream and a System.IO.StreamReader, that implement IDisposable and are not disposing the objects immediately after use. Yeah sure the finalizer will take care of any unmanaged resources at the end of the day but why not just release the resources in a predictable and up front manner. I have recommended that Microsoft use the following instead.

    using (System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("Project1.UserControl1.xaml"))
    {
        using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
        {
            this.InitializeFromXaml(sr.ReadToEnd());
        }
    }

If you feel strongly about best practices, please take a moment and vote for this on the feedback site here.

Cross-posted from CraigN.NET.