Release resources in .Net C#

前端 未结 6 1485
庸人自扰
庸人自扰 2021-02-19 11:19

I\'m new to C# and .NET, ,and have been reading around about it.

I need to know why and when do I need to release resources? Doesn\'t the garbage collector take care of

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 12:08

    You only need to worry about precious resources. Most objects you create while programming do not fit into this category. As you say, the garbage collector will take care of these.

    What you do need to be mindful of is objects that implement IDisposable, which is an indication that the resources it owns are precious and should not wait for the finalizer thread to be cleaned up. The only time you would need to implement IDisposable is on classes that own a) objects that implement IDisposable (such as a file stream), or b) unmanaged resources.

提交回复
热议问题