为什么使用比使用更好?

发布于 2024-10-17 20:53:57 字数 434 浏览 2 评论 0原文

根据此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻安 2024-10-24 20:53:57

您也可以控制何时使用 use 调用 dispose,只需使用常用的作用域结构(如 parens 或 begin-end),例如

let F() =
    let x = 4
    (
        use file = System.IO.File.Open("foo.xml", System.IO.FileMode.Append)
        let z = 4
        printfn "file still open here"
    )
    printfn "file was already closed/disposed"

但是我认为这很少有用。我认为不想命名/使用 IDisposable 对象的情况也很少见。 use 在语法上更方便,95% 的时间都可以满足您的需要,所以我认为这就是为什么它是首选的。

You can control when dispose is called with use as well, just by using usual scoping contructs (like parens or begin-end), e.g.

let F() =
    let x = 4
    (
        use file = System.IO.File.Open("foo.xml", System.IO.FileMode.Append)
        let z = 4
        printfn "file still open here"
    )
    printfn "file was already closed/disposed"

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.

萌辣 2024-10-24 20:53:57

我认为更喜欢 use 的原因只是语法更简单。许多其他语言结构可以表示为函数(例如,try .. withforwhile、...)。如果语言设计者添加了更简单的语法,为什么不使用它......

正如我在 您引用的早期答案,即使使用use,您也可以精确控制范围。 (这样,您甚至可以在 对象表达式 类声明的构造函数中使用它。)但大多数时候,自动行为就很好(这使得构造比 using< /code> 在 C# 中)。

在需要显式控制范围的情况下,是否使用 use 还是 using 取决于个人喜好。如果您不喜欢 use 的显式范围(我承认这看起来有点奇怪,但对我来说效果很好),您可以使用 using

编辑:在类声明中,您不能编写:

type Foo() =
  use a = new Whatever()
  // ...

因为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 of object expressions class declarations.) But most of the time, the automatic behavior is just fine (which makes the construct simpler than using in C#).

Whether you'll use use or using in situations where you need to control the scope explicitly is a matter of personal taste. If you don't like the explicit scoping of use (which looks a bit weird, I admit, but works fine for me), you can use using.

EDIT: In a class declaration, you cannot for example write:

type Foo() =
  use a = new Whatever()
  // ...

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 of IDisposable to your type). If you use using, you don't get this sort of trouble.

情归归情 2024-10-24 20:53:57

就我个人而言,我更喜欢 use 而不是 using,这与我更喜欢使用绑定形式相同

let a = some_expr
some_stuff_with_a

(fun a -> some_stuff_with_a) some_expr

您通常可以避免一组括号,以及标识符和它所绑定的值在空间上更接近并且更容易看到。

Personally, I prefer use to using for the same reason that I prefer

let a = some_expr
some_stuff_with_a

to

(fun a -> some_stuff_with_a) some_expr

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.

潦草背影 2024-10-24 20:53:57

反对 useusing 更好的例子:

usinguse 更好,因为 using > 可以写成一行,而 use 则不能。

示例,
xx 是一个通过函数 fct 从由 yy 使用给定参数 p 打开的资源中返回值的函数。

let xx p = using (yy(p)) (fun resource-> fct resource)     // <-- this is OK

let xx p  = (use resource = yy(p); fct resource)           // <-- this is Not OK

An example against use is better then using:

using is better than use as using can be written in one line while use cannot.

Example,
xx is a function returning a value by a function fct from a resource which is opened by yy using given parameter p.

let xx p = using (yy(p)) (fun resource-> fct resource)     // <-- this is OK

let xx p  = (use resource = yy(p); fct resource)           // <-- this is Not OK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文