I've been dealing with heavy use of Stream types recently. Not as in the fairly common usages of say, reading a text file or whatnot. We're talking Streams that transform data from one format to the other transparently to the caller, that run across various nodes in a cluster (think a Stream of Streams), that sort of deal. So I found it a pleasant situation where I actually needed to perform some simple good-enough alerting of some operations team when some email messages needed to be intercepted by our system and the set of attachments actually resent along in another email. Nothing too crazy here. However if anyone has ever used the MailAttachment class before, they should know that the overload that accepts another stream doesn't actually read it until you send the email message itself. Not so much a problem except when you like to clean up behind yourself but what you are doing is operating on a tight unbounded loop and the references to the Stream instances are highly ephemeral. That Stream might be expensive after all. What I really needed to do was dispose of them at a defined point in the future but without cluttering up the the admittedly simple code I need to accomplish with cruft about issues orthogonal to what I am doing. So I whipped out this simple component that can be scoped to a region of code that you can rely on to clean up once you exit the block.

Here's an example of mis-use that looks correct and how the code should seemingly work:

var anEmail = new MailMessage(yadaYadaYada); // settings cruft omitted for brevity
foreach (var thingToEmail in GetThingsToEmail())
{
  using (var stream = thingToEmail.OpenStream())
  {
    var attachment = new Attachement(stream);
    anEmail.Attachments.Add(attachment);
  }
}
anSmtpClient.Send(anEmail); // BOOM-Streams are closed so exception is thrown

Well that sucks, right? We could write some ugly try..finally code and keep a bag of the streams and then iterate them in the finally and call dispose
OR
we can keep the simple pattern above and still work with the API limitations with this handy little component:

var anEmail = new MailMessage(yadaYadaYada); // settings cruft omitted for brevity
using (var scope = new AggregateDisposable())
{
  foreach (var thingToEmail in GetThingsToEmail())
  {
    var stream = thingToEmail.OpenStream();
    scope.Track(stream);
    var attachment = new Attachement(stream);
    anEmail.Attachments.Add(attachment);
   }
  anSmtpClient.Send(anEmail); // Happy happy Joy joy
}

And Bob's your uncle. Just because I happen to be a very nice guy I have provided a nice overload that actually allows you to materialize the IDisposable component, track it, and set it to your choice of variable in one go like this=>
var stream = scope.Track(() => thingToEmail.OpenStream());

Well there you go. Enjoy!