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.

[tags]Visual Studio,Silverlight,best practices[/tags]

Be Sociable, Share!