Continuing in our weekly blog post series that highlights a few of the new image[5]_thumb[2]_thumb[2]_thumbadditions to the Framework Design Guidelines 2nd edition.. This content is found in the Factored Types section of Chapter 9: Common Design Patterns. Phil offers some great additions to the base pattern.

PHIL HAACK

Since Factored Types have an explicit lifetime, it probably makes good sense to implement the IDisposable interface so that developers can make use of the using statement. The code sample here could then be refactored to:

using(SerialPort port = new SerialPort("COM1")) {
  
port.Open();
  
GZipStream compressed;
  
compressed = new GZipStream(port.BaseStream,
      CompressionMode.Compress);
   
compressed.Write(data, 0, data.Length);
}