Skip to content

Instantly share code, notes, and snippets.

@haacked
Last active December 10, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save haacked/4507192 to your computer and use it in GitHub Desktop.
Save haacked/4507192 to your computer and use it in GitHub Desktop.
public sealed class Dependency : IDisposable
{
public string SomeProperty { get; set; }
public void Dispose()
{
}
}
public sealed class Owner : IDisposable
{
readonly Dependency dependency;
public Owner()
{
// This will violate CA2000 Dispose objects before losing scope
dependency = new Dependency { SomeProperty = "Blah" };
}
public void Dispose()
{
dependency.Dispose();
}
}
//Here's what it generates
public sealed class Owner : IDisposable
{
// Fields
private Dependency dependency;
// Methods
public Owner()
{
// <>g__initLocal0 is never disposed.
Dependency <>g__initLocal0 = new Dependency {
SomeProperty = "Blah"
};
this.dependency = <>g__initLocal0;
}
public void Dispose()
{
this.dependency.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment