C# 使用 {} 语句在内部调用 return 是一种好方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11776945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it a good approach to call return inside using {} statement?
提问by
I just want to know is it safe/ good approach to call returninside a usingblock.
我只想知道return在using块内调用是否安全/好方法。
For ex.
例如。
using(var scope = new TransactionScope())
{
// my core logic
return true; // if condition met else
return false;
scope.Complete();
}
We know the at the last most curly brace dispose()will get called off. But what will be in the above case, since returnjumps the control out of the given scope (AFAIK)...
我们知道最后一个大括号 dispose()将被取消。但是在上述情况下会发生什么,因为return将控制权跳出给定范围(AFAIK)......
- Is my
scope.Complete()get called? - And so for the scope's
dispose()method.
- 我的
scope.Complete()被调用了吗? - 作用域的
dispose()方法也是如此。
采纳答案by ?yvind Br?then
It's perfectly safe to call returninside your usingblock, since a using block is just a try/finallyblock.
return在using块内调用是完全安全的,因为 using 块只是一个try/finally块。
In your example above after return true, the scope will get disposed and the value returned. return false, and scope.Complete()will notget called. Disposehowever will be called regardless since it reside inside the finally block.
在上面 return 之后的示例中true,范围将被处理并返回值。return false,并且scope.Complete()将不会被调用。Dispose但是无论如何都会被调用,因为它位于 finally 块中。
Your code is essentially the same as this (if that makes it easier to understand):
您的代码与此基本相同(如果这样更易于理解):
var scope = new TransactionScope())
try
{
// my core logic
return true; // if condition met else
return false;
scope.Complete();
}
finally
{
if( scope != null)
((IDisposable)scope).Dispose();
}
Please be aware that your transaction will nevercommit as there's no way to get to scope.Complete()to commit the transaction.
请注意,您的事务永远不会提交,因为无法scope.Complete()提交事务。
回答by Lucero
That's fine - finallyclauses (which is what the closing curly brace of the usingclause does under the hood) always get executed when the scope is left, no matter how.
这很好 -finally子句(这是using子句的右花括号在幕后所做的)总是在离开作用域时执行,无论如何。
However, this is only true for statements that are in the finally block (which cannot be explicitly set when using using). Therefore, in your example, scope.Complete()would never get called (I expect the compiler to warn you about unreachable code though).
但是,这仅适用于 finally 块中的语句(使用 时无法显式设置using)。因此,在您的示例中,scope.Complete()永远不会被调用(尽管我希望编译器会警告您无法访问的代码)。
回答by M. Mennan Kara
In general, it is a good approach. But in your case, if you return before calling the scope.Complete(), it will just trash the TransactionScope. Depends to your design.
总的来说,这是一个很好的方法。但是在您的情况下,如果您在调用 之前返回scope.Complete(),它只会破坏 TransactionScope。取决于你的设计。
So, in this sample, Complete() is not called, and scope is disposed, assuming it is inheriting IDisposable interface.
所以,在这个示例中,Complete() 没有被调用,并且作用域被释放,假设它继承了 IDisposable 接口。
回答by Aristos
To make sure that the scope.Complete()will be called, wrap it with try/finally. The disposeis called because you have wrap it with the usingthat is an alternative try/finallyblock.
为确保scope.Complete()将被调用,请使用try/finally. 之所以dispose被调用,是因为您已将其与using作为替代try/finally块的包装在一起。
using(var scope = new TransactionScope())
{
try
{
// my core logic
return true; // if condition met else
return false;
}
finally
{
scope.Complete();
}
}
回答by ThunderGr
In this example, scope.Complete() will never execute. However, the return command will cleanup everything that is assigned on the stack. The GC will take care of everything that is unreferenced. So, unless there is an object that can not be picked up by the GC, there is no problem.
在这个例子中,scope.Complete() 永远不会执行。但是,返回命令将清除堆栈上分配的所有内容。GC 将处理所有未引用的内容。所以,除非有一个对象不能被 GC 拾取,否则没有问题。
回答by Tony Kh
scope.Complete should definitely be called before return. Compiler will display a warning and this code will never be called.
scope.Complete 绝对应该在 之前调用return。编译器将显示警告,并且永远不会调用此代码。
Regarding returnitself - yes, it is safe to call it inside usingstatement. Using is translated to try-finally block behind the scene and finally block is to be certainly executed.
关于return它本身 - 是的,在using语句中调用它是安全的。using 被翻译成 try-finally 块在幕后,finally 块肯定会被执行。
回答by daryal
In the example you have provided, there is a problem; scope.Complete()is never called.
Secondly, it is not a good practice to use returnstatement inside usingstatements. Refer to the following:
在您提供的示例中,存在问题;scope.Complete()永远不会被调用。其次,return在using语句中使用语句不是一个好习惯。请参阅以下内容:
using(var scope = new TransactionScope())
{
//have some logic here
return scope;
}
In this simple example, the point is that; the value of scopewill be null when using statement is finished.
在这个简单的例子中,重点是;scope当 using 语句完成时,的值为空。
So it is better not to return inside using statements.
所以最好不要在 using 语句里面返回。

