-
Notifications
You must be signed in to change notification settings - Fork 4
Gendarme.Rules.Correctness.EnsureLocalDisposalRule(2.10)
Sebastien Pouliot edited this page Jan 22, 2011
·
2 revisions
Assembly: Gendarme.Rules.Correctness
Version: 2.10
This rule checks that disposable locals are always disposed of before the method returns. Use a 'using' statement (or a try/finally block) to guarantee local disposal even in the event an unhandled exception occurs.
Bad example (non-guaranteed disposal):
void DecodeFile (string file)
{
var stream = new StreamReader (file);
DecodeHeader (stream);
if (!DecodedHeader.HasContent) {
return;
}
DecodeContent (stream);
stream.Dispose ();
}
Good example (non-guaranteed disposal):
void DecodeFile (string file)
{
using (var stream = new StreamReader (file)) {
DecodeHeader (stream);
if (!DecodedHeader.HasContent) {
return;
}
DecodeContent (stream);
}
}
Bad example (not disposed of / not locally disposed of):
void DecodeFile (string file)
{
var stream = new StreamReader (file);
Decode (stream);
}
void Decode (Stream stream)
{
/*code to decode the stream*/
stream.Dispose ();
}
Good example (not disposed of / not locally disposed of):
void DecodeFile (string file)
{
using (var stream = new StreamReader (file)) {
Decode (stream);
}
}
void Decode (Stream stream)
{
/*code to decode the stream*/
}
- This rule is available since Gendarme 2.4
Note that this page was autogenerated (3/17/2011 9:31:58 PM) based on the xmldoc
comments inside the rules source code and cannot be edited from this wiki.
Please report any documentation errors, typos or suggestions to the
Gendarme Mailing List. Thanks!