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]
I’m voting right now and digging it. These type of things are really important from a Best Practice point of view. Surely the people behind designing the language should lead by example!
Good observation!
–
Punit Ganshani