关于从使用块返回的最佳实践
哪种方法更好:从 using
语句内的方法返回一个值,或者在之前声明一个变量,在内部设置它并在之后返回它?
public int Foo()
{
using(..)
{
return bar;
}
}
或者
public int Foo()
{
var b = null;
using(..)
{
b = bar;
}
return b;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自使用声明 - MSDN
来自 try-finally(C# 参考)
要回答您的问题,是的,可以从 using 语句返回。
From using Statement - MSDN
From the try-finally (C# Reference)
To answer your question, yes its okay to return from a using statement.
我更喜欢第一个例子。 更少的变量,更少的代码行,更容易遵循,更容易维护......
I prefer the first example. Fewer variables, fewer lines of code, easier to follow, easier to maintain...
遵循“少即是多”原则(实际上只是 KISS 的变体),前者。 需要维护的代码行更少,语义没有变化,也没有可读性损失(可以说这种风格更容易阅读)。
Following the "less is more" principle (really just a variant of KISS), the former. There are fewer lines of code to maintain, no change in semantics and no loss of readability (arguably this style is easier to read).
第二种显然更好,你可以通过编写测试程序来验证它是否正常工作。
using
语句本身不能有值,这是一个限制。 假设您有一个名为Open
的方法,它返回一个打开的FileStream
,并且您想要获取文件的长度:这里的错误是您没有处理 <代码>文件流。 所以你必须写(类似于你的例子):
但是用 简单的扩展方法,你可以这样写:
漂亮又整洁,
FileStream
得到正确处理。The second is clearly better, and you can verify that it works fine by writing a test program.
The
using
statement itself can't have a value, which is a limitation. Suppose you have a method calledOpen
that returns an openFileStream
, and you want to get the length of the file:The bug there is that you aren't disposing the
FileStream
. So you have to write (similar to your example):But with a simple extension method, you can write this instead:
Nice and neat, and the
FileStream
gets properly disposed of.没有理由不这样做,因为
using
语句会转换为try...finally
块,并且finally
部分保证被执行(即使通过返回或未处理的异常)。No reason not to since that
using
statement translates into atry...finally
block and thefinally
part is guaranteed to be executed (even through a return or an unhandled exception).这确实取决于个人喜好。 你会发现这个特定栅栏两边都有争论。 我个人更倾向于选择 1:尽快返回。 我相信它更好地表达了代码的意图; 没有理由坚持比你必须坚持的时间更长的时间。 如果您已完成所有工作,请返回。
有时,您将有多个可能的返回点,并且“方法结束”工作(日志记录、清理)可能会导致您得到单个返回语句。 这没什么可怕的,但是您通常可以在
finally
块中或使用面向方面编程中的方面来处理这些情况。It really comes down to personal preference. You'll find arguments on both sides of this particular fence. Myself, I favor option 1: returning as soon as you can. I believe it expresses the intent of the code better; there's no reason to stick around longer than you have to. If you've completed all your work, return.
Sometimes, you'll have multiple possible return points, and "end-of-method" work (logging, cleanup) that might lead you to a single return statement. Nothing terrible about that, but you can often handle those situations in
finally
blocks or with aspects in aspect-oriented programming.我觉得第二个更好,
使用这种方式时想到的一件事是,我们在 using 之间返回,所以对象(我们包装在 using 中)将被处置,答案是肯定的
因为 using 语句只是 try/finally 块的混合,所以从 try 块返回也可以。将计算返回表达式,然后执行 finally 块,然后该方法将返回。所以继续:)
I feel second one better
One thing that arises in mind while using this way is that we are returning in between the using so will the object(that we have wrapped in using ) will get disposed ,the Answer is yes
because A using statement is just the mixture of try/finally block, it's fine to return from a try block too.The return expression will be evaluated, then the finally block will be executed, and the the method will return then.So go Ahead:)