Does the “using” keyword mean the object is disposed and GC'ed?

前端 未结 2 1349
死守一世寂寞
死守一世寂寞 2021-01-01 09:11

I struck up a conversation with my colleague today, who said she\'d just learned the reason behind using the using statement.

 //Using keyword i         


        
2条回答
  •  不思量自难忘°
    2021-01-01 09:45

    She is right on the money using statement is just a syntatic sugar for try/finally{obj.Dispose();} . using statement ensures that the object will be disposed.(Dispose method will be called) but it has no relationship with garbage collection.

    Take a look at this Understanding-the-using-statement

    Short answer: So now we know using statement is just calling Dispose and does nothing other than that, and keep in mind that Dispose method is no special than any other methods. It is just a method and that's it. So it is no way related to garbage collection. Interestingly "Garbage Collector" doesn't even know about Dispose method or IDisposable.

    Hope this helps

提交回复
热议问题