Difference between instantiating class and using

Asked

Viewed 252 times

11

I can instantiate a class in two ways in C#, they being:

HttpClient http = new HttpClient();

or

using (var http = new HttpClient())
{
    //
}

Is there any difference between these means? Performance? Good practice?

  • You can only use using in disposable classes.

2 answers

14


The question itself doesn’t make as much sense because in both is instantiating a class, in exactly the same way other than the fact that one of them is using a var that could have used in both places.

The difference is the using. With it you ensure that the object will call a method of closing the operation at the end of the block, no matter what happens, it can even be when it gives an error and generates an exception. When it does not do this it has to take care of this problem manually, which is wrong and often almost impossible, even if it is possible almost nobody knows how to do right.

This is only needed on objects that need this closure, so they implement the interface IDisposable who has the method Dispose() which will be automatically called by using at the right time. This is called Disposable Pattern.

There’s a lot about it here:

The object remains available, it had a discard of the situation that operates at that time, the destruction of the object only occurs by Garbage Collector, then it is possible to even make a resurrection of him so it is possible to even make a resurrection of him (there was an answer that stated otherwise, probably why I received a negative here).

There is no such thing as good practice (which is just a recipe for cake for those who do not understand what is there), this is used when it is necessary, when it is what you need and want to do. The performance of the former is slightly better, but it is wrong in most situations, so there is no point in comparing.

  • 2

    Did anyone see anything wrong in my answer? Or was the negative given out of revenge?

  • I don’t think it’s wrong to use the Dispose() directly, you can say that it is preferable to use the using where possible. There are cases where it is necessary to have Idisposable in a larger scope than a method. Of course in such cases the class that uses it must/must implement the Idisposable interface.

  • @ramaral I do not know if I understand, here I do not say this, however it is almost wrong, because almost always the person will call wrong, and almost never has any advantage even doing right. Of course it is not wrong in 100% of cases. And I do not know if I understood the scope of the method, that is to say that there are cases that the object Disposable can be in a field and not locally and there the using Isn’t that right? If that’s something I’ve always wanted to write but never had a specific question. I speak from the top in some answers including the one I gave today, but not specifically, it’s always "on the air".

  • I mean the phrase: "When you don’t do it you have to take care of that problem manually, what is wrong and often almost impossible". I understood in it that you meant the call "manual" of Dispose(). There are cases where interaction with Idisposable requires it to be "always" available and not just during the call to a method.

  • 1

    But I’m not talking about Dispose() in itself, and rather call some other method that closes. But it may be the Dispose() if you do wrong because if you make an exception he will not be called (if you do it naively) or you can even call when you have already been called.

  • Yes, I understand. The problem is not using Dispose() but using it without knowing what is being done. The way to ensure that he is called always, at the right time and in the right way is to use using. I’m currently using an API(Simconnect] where it is not possible to using. This forced me to study the Idisposable interface and how to implement it correctly.

  • @ramaral yes, there are cases like this, although most of the time is bad implementation, I know nothing about this I can not talk.

Show 2 more comments

13

In both cases you create an instance but using the using, a scope is created that, at the end of its execution, releases resources automatically through the method Dispose().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.