相关疑难解决方法(0)

从内部使用块返回是否可以

我正在进行代码审查,并找到了许多具有以下格式的代码:

public MyResponse MyMethod(string arg)
{
    using (Tracer myTracer = new Tracer(Constants.TraceLog))
    {
        MyResponse abc = new MyResponse();

        // Some code

        return abc;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码分析时,我得到一个CA2000警告Microsoft.Reliability

应该将代码重写为:

public MyResponse MyMethod(string arg)
{
   MyResponse abc = new MyResponse();

   using (Tracer myTracer = new Tracer(Constants.TraceLog))
   {
       // Some code
   }
   return abc;
}
Run Code Online (Sandbox Code Playgroud)

或者没关系?

编辑

报告警告的行是:

MyResponse abc = new MyResponse();
Run Code Online (Sandbox Code Playgroud)

MyResponse是标准的数据集.

完整的错误消息是:

警告150 CA2000:Microsoft.Reliability:在方法'xxxxx(Guid,Guid)'中,对象'MyResponse'未沿所有异常路径放置.在对所有引用超出范围之前,在对象'MyResponse'上调用System.IDisposable.Dispose.

c# return

27
推荐指数
2
解决办法
2087
查看次数

标签 统计

c# ×1

return ×1