为什么使用比使用更好?
根据此 MSDN 页面 使用 优于
using
。我在其他地方听到过(例如这个答案)。这是为什么呢?我意识到 use
是后来添加的。但有什么区别呢?从表面上看,using
似乎更有用,因为您可以控制何时调用 Dispose()
,并且可以显式忽略绑定值(例如,(fun _ -> ...)
) 如果需要的话。
According to the last sentence on this MSDN page use
is to be preferred over using
. I've heard it elsewhere (this answer, for example). Why is this? I realize use
was added later. But what's the difference? On the surface, using
seems more useful because you can control when Dispose()
is called, and you can explicitly ignore the bound value (e.g., (fun _ -> ...)
) if needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您也可以控制何时使用
use
调用 dispose,只需使用常用的作用域结构(如 parens 或begin
-end
),例如但是我认为这很少有用。我认为不想命名/使用 IDisposable 对象的情况也很少见。
use
在语法上更方便,95% 的时间都可以满足您的需要,所以我认为这就是为什么它是首选的。You can control when dispose is called with
use
as well, just by using usual scoping contructs (like parens orbegin
-end
), e.g.But I think this is rarely useful. I think it is also rare to not want to name/utilize the
IDisposable
object.use
is more syntactically convenient, and 95% of the time does what you need, so I think that's why it's preferred.我认为更喜欢
use
的原因只是语法更简单。许多其他语言结构可以表示为函数(例如,try .. with
、for
、while
、...)。如果语言设计者添加了更简单的语法,为什么不使用它......正如我在 您引用的早期答案,即使使用
use
,您也可以精确控制范围。 (这样,您甚至可以在对象表达式类声明的构造函数中使用它。)但大多数时候,自动行为就很好(这使得构造比using< /code> 在 C# 中)。
在需要显式控制范围的情况下,是否使用
use
还是using
取决于个人喜好。如果您不喜欢use
的显式范围(我承认这看起来有点奇怪,但对我来说效果很好),您可以使用using
。编辑:在类声明中,您不能编写:
因为
a
的范围将(可能)是实例的整个生命周期。 (尽管我认为这可能很有用,并且它可以将IDisposable
自动实现添加到您的类型中)。如果你使用using
,你就不会遇到这种麻烦。I think that the reason for preferring
use
is just that the syntax is simpler. Many other language constructs could be expressed as functions (e.g.try .. with
,for
,while
, ...). If the language designers added a simpler syntax, why not use it...As I wrote in the earlier answer you referenced, you can precisely control the scope even when using
use
. (And this way, you can use it even in constructors ofobject expressionsclass declarations.) But most of the time, the automatic behavior is just fine (which makes the construct simpler thanusing
in C#).Whether you'll use
use
orusing
in situations where you need to control the scope explicitly is a matter of personal taste. If you don't like the explicit scoping ofuse
(which looks a bit weird, I admit, but works fine for me), you can useusing
.EDIT: In a class declaration, you cannot for example write:
because the scope of
a
would be (possibly) the whole lifetime of the instance. (Although I think this could be useful and it could add automatic implementation ofIDisposable
to your type). If you useusing
, you don't get this sort of trouble.就我个人而言,我更喜欢
use
而不是using
,这与我更喜欢使用绑定形式相同,
您通常可以避免一组括号,以及标识符和它所绑定的值在空间上更接近并且更容易看到。
Personally, I prefer
use
tousing
for the same reason that I preferto
With the binding form, you can typically avoid a set of parentheses, and the association between the identifier and the value that it's being bound to are closer in space and easier to see.
反对
use
比using
更好的例子:using
比use
更好,因为using
> 可以写成一行,而use
则不能。示例,
xx
是一个通过函数 fct 从由yy
使用给定参数p
打开的资源中返回值的函数。An example against
use
is better thenusing
:using
is better thanuse
asusing
can be written in one line whileuse
cannot.Example,
xx
is a function returning a value by a function fct from a resource which is opened byyy
using given parameterp
.