0

please consider this code :

1-...
2-{
  3-...
  4-SqlConnection cn=new SqlConnection();
  5-... 
6-}

if program reach to statement No.6 will cn dispose? if not what happend for cn variable?

Arian
  • 12,793
  • 66
  • 176
  • 300
  • possible duplicate of [C# - Are objects immediately destroyed when going out of scope?](http://stackoverflow.com/questions/1480868/c-sharp-are-objects-immediately-destroyed-when-going-out-of-scope) – Damith Jun 17 '12 at 10:22

5 Answers5

5

if program reach to statement No.6 will cn dispose?

No.

if not what happend for cn variable?

It depends whether you use it afterwards. If you don't use it and it falls out of scope which means that it is eligible for garbage collections. When this garbage collection happens is out of your control. But when it happens the destructor will be invoked which itself calls the Dispose method.

This being said the correct approach is to always wrap IDisposable resources in using statements. This ensures that the Dispose method will always be called, even if an exception is thrown:

2-{
  3-...
  4-using (SqlConnection cn=new SqlConnection())
    {
        ...
    }
    5-... 
6-}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Dimtrov: thanks dear friend consider it is in a function and I declare and use this variable in others function.Now what happened for `cn` variable – Arian Jun 17 '12 at 10:29
  • Does connection will close if I don't dispose `cn` variable? – Arian Jun 17 '12 at 10:51
  • 1
    No, the connection will be returned to the ADO.NET connection pool so that it can be reused. Opening physical database connections is an expensive operation. For this reason ADO.NET keeps a pool of connections. When you call the Dispose method you are returning the connection to the pool so that it can be reused. And when you create a new SqlConnection using the constructor you are not opening a physical connection to the database. You are just drawing an existing connection from the pool. Failing to return the connection to the pool will lead to connection exhaustion. – Darin Dimitrov Jun 17 '12 at 11:09
  • Thanks but I think I see a mismatch in your answer and @Guffa answer.He said that connection would close after disposed object but you said that connection return to connection pool.In one of my program and I got `Timeout expired` errors and this happened for `select` statement.can this error because of unclosed connections? – Arian Jun 18 '12 at 08:02
  • Yes, there is a mismatch indeed. I continue to support my answer. You can read more about connection pooling here: http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx. As far as your timeout problem => since you are not disposing the connection properly (by wrapping it in a `using` statement) this connection is not returned to the connection pool. So after a couple of calls to this method there are no longer any connections left in the pool and your code will stop working. – Darin Dimitrov Jun 18 '12 at 08:06
2

No, it will be disposed when GarbageCollector decides to destroy it.

If you want to dispose it then use using statement (as SqlConnection implements IDisposeable):

using(SqlConnection cn = new SqlConnection())
{
    // code
}
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
2

No, the object will not be disposed when the variable goes out of scope.

When the object is not used any more (which is somewhere inside the scope, after the last line that uses the variable), the object is up for garbage collection.

The next time the garbage collector runs, it will find that the object is not used any more, but it will also find that it has a Finalize method, and that it's not disposed, so the object will be added to the finaliser queue. Also, as the object is not collected, it will likely be moved to the next heap generation, which involves actually moving the object from one place in memory to another.

The garbage collector has a special thread that finalises objects from the finaliser queue, which will eventually finalise the object. The Finalize method of the object will call the Dispose method, which closes the database connection.

After the object has been disposed, it will eventually be collected by the garbage collector.

Note that there is no gurantee that the object will be finalised within any specific time, or even finalised at all.

So, if you don't dispose the connection object, the two main things that happen is:

  • You have no control over when the connection is closed, if ever.
  • Finalising an object is a much more expensive process than calling Dispose directly.
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Does connection will close if I don't dispose `cn` variable? – Arian Jun 17 '12 at 10:50
  • 1
    @Kerezo: The connection will be closed when (and if) the garbage collector disposes the object, or when the database determines that it has been unused for too long (several minutes). – Guffa Jun 17 '12 at 11:15
1

You should use a Using statement, but the result will be the same it wont be disposed it will be marked as disposable and the GC will collect it the next time it runs.

BigL
  • 1,631
  • 1
  • 11
  • 10
0

No Kerezo,

1-... 
2-{ 
  3-... 
  4-SqlConnection cn=new SqlConnection(); 
  5-...  
6-} 

We the compiler reaches the Statement cn object is not destroyed.You can't access the "cn" object out of the loop.

If you use the Using block then the object will be disposed automatically.

Hemant Kumar
  • 4,593
  • 9
  • 56
  • 95