-1

Consider the following example.

public void SomeMethod(){
 using(var sqlConnection= new SQLConnection()){
 //some code here
 }
}

In the above example the sqlConnection outside the using block will be disposed and garbage collected

public void SomeMethod(){
var sqlConnection = new SQLConnection(){
}
}

In this example the sqlConnection object will be garbage collected after the execution of SomeMethod().

The question here is, is it really necessary to use a using() scope in this case as it is ok for me to have the object garbage collected at the end of execution. Could some one share your thoughts here.

mjwills
  • 23,389
  • 6
  • 40
  • 63
arun thatham
  • 500
  • 1
  • 4
  • 13
  • Just an observation, the `sqlConnection` will be garbage collected *at some point* after the method execution. The `using` statement makes sure that the `Dispose` method of that object will be called deterministically rather than waiting for GC execution to dispose and collect it. – IPValverde Mar 15 '19 at 08:34
  • 3
    `object will be garbage collected after` **after** is not the same as **immediately after**. Technically I die after I was born. **But how long after?** – mjwills Mar 15 '19 at 08:36

2 Answers2

3

IDisposable has nothing in common with garbage collection. It is just a contract saying that you should call Dispose on an object implementing it because it needs to make some cleanup. using statement is a helping tool that just makes that for you. Moreover, various tools (i.e. Resharper) may notice that you are not calling Dispose (or use using) on such Disposable object.

Garbage collection is just the other mechanism that will reclaim memory after no longer needed objects. In your case, SQLConnection will be garbage collected after some time the method exits (because it will notice that nothing is referencing it anymore).

Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
1

The general answer is: of course it's preferable and recommended to use using with IDisposable object because it ensures some expected behavior, but the necessity of using may depend on other factors such as: correct implementation of IDisposable pattern and absence of need to reclaim the resources immediately (maybe because they will be reclaimed shortly anyway in short-term console application or because of any other reason known only for you). But considering fact that in vast majority of cases these factors are unknown we prefer to take precautions by applying using always where it's possible.

To be more specific the correctly implemented IDisposable pattern ensures that unmanaged resources (which IDisposable is about) are released either during explicit Dispose call (that is when an object leaves using scope) or during finalization phase (that is when an object is collected by GC). In your case SQLConnection type has correct IDisposable implementation so applying or not applying using in this specific case is almost identical. Almost - because you still cannot be sure when GC starts collecting your object and you cannot handle the situation when Dispose throws an exception. That's why the recommendation and the best practice is to use using anyway but your question was about necessity in your specific case so basing on the invormation above - you decide.

Dmytro Mukalov
  • 1,949
  • 1
  • 9
  • 14