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.
Used to refers to something familiar or routine, as in "I'm used to getting up early for work," or to say that something repeatedly happened in the past like "we used to go out more." Use to typically occurs with did; "did you use to work there?" or "it didn't use to be like that," describing something in the past that ...
Therefore, use can always be used, but utilize should only be used when indicating a creative use. Some writers throw in the word “utilize” to sound smart, but when doing so in the wrong context, they sound anything but.
The words employ and utilize are common synonyms of use. While all three words mean "to put into service especially to attain an end," use implies availing oneself of something as a means or instrument to an end.
In this page you can discover 39 synonyms, antonyms, idiomatic expressions, and related words for because, like: since, due-to, for the reason that, as a result of, therefore, by reason of, as, on-account-of, in consequence of, in-behalf-of and for.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With