Is it OK doing a return from inside using block
c#, return
Solution
Your rewrite will not fix that CA2000 warning, because the problem is not the `Tracer` object, but the `MyResponse` object. The documentation states:
The following are some situations where the using statement is not enough to protect IDisposable objects and can cause CA2000 to occur. Returning a disposable object requires that the object is constructed in a try/finally block outside a using block.
To fix the warning without messing with the stack trace of your exceptions (<- click, it's a link), use this code:
public MyResponse MyMethod(string arg)
{
MyResponse tmpResponse = null;
MyResponse response = null;
try
{
tmpResponse = new MyResponse();
using (Tracer myTracer = new Tracer(Constants.TraceLog))
{
// Some code
}
response = tmpResponse;
tmpResponse = null;
}
finally
{
if(tmpResponse != null)
tmpResponse .Dispose();
}
return response;
}
Why? Please see the example in the linked documentation.
Problem
I am doing a code review, and have found alot of code with the following format: ``` public MyResponse MyMethod(string arg) { using (Tracer myTracer = new Tracer(Constants.TraceLog)) { MyResponse abc = new MyResponse(); // Some code return abc; } } ``` When I run a code analysis I get a CA2000 warning Microsoft.Reliability Should the code be rewritten as: ``` public MyResponse MyMethod(string arg) { MyResponse abc = new MyResponse(); using (Tracer myTracer = new Tracer(Constants.TraceLog)) { // Some code } return abc; } ``` Or does it not matter? Edit The line on which it is reporting the warning is: ``` MyResponse abc = new MyResponse(); ``` MyResponse is a standard Dataset. The full error message is: Warning 150 CA2000 : Microsoft.Reliability : In method 'xxxxx(Guid, Guid)', object 'MyResponse ' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'MyResponse ' before all references to it are out of scope.