Disposing of unmanaged objects in c#

左心房为你撑大大i 提交于 2020-02-05 05:50:20

问题


Let's say I have a class that has a MyCustomDatabaseAccess as a data member. MyCustomDatabaseAccess has a Dispose() method. MyCustomDatabaseAccess is the middleware class that accesses the database.

public class MyClass {
   private MyCustomDatabaseAccess db_access;
}

Does MyClass need to implement IDisposable interface?

My solution right now is to have do something like this:

    public class MyClass {
       private MyCustomDatabaseAccess db_access;

       public void GetDBResults () {
          db_access = new MyCustomDatabaseAccess();
          DataTable dt = db_access.ExecuteStoredProc(param1, param2, etc..);

          //do stuff with results

          db_access.Dispose();

    }

}

From what I read on MSDN, another way to make sure that this object is disposed of properly would be to have MyClass implement IDisposable interface, then implement a Dispose() function, then call it in the class that calls an object of MyClass. see this for more info http://www.devx.com/dotnet/Article/33167/0/page/3

Which way is preferable and why? thanks!


回答1:


I think you just need to ensure your objects are disposed.

if you are doing this in your method, even if there is an unexpected error, then I think you are ok. If you hold on to unmanaged resources in your class then implementing IDisposable is a way to ensure that you get a chance to dispose resources when your object is finalized, or to give your users a way to dispose of the resources explicitly.

If you are only creating and using the resources in a method and not holding references in the class then as long as you are ensuring they are disposed in the method (either by doing it manually , or more easily, by wrapping then in a using block), then you should be ok I think.




回答2:


I would suggest using one of the following two approaches:

  1. Wrap the IDisposable object in a using block.
  2. Keep a reference to the IDisposable object inside your class and have your class implement IDisposable. Dispose the object in your class's Dispose method.

Which you should choose depends on the required lifetime of the object.

  • If you only need it for the duration of a single method call then the first option - a using block - is preferable because it is simpler and harder to get wrong. The using block (almost) guarantees that Dispose will be called for you as soon as the object is no longer needed - even in the case where an exception is thrown.
  • If the disposable object must last longer than the duration of a single method call then you cannot use the first option, so you should use the second - i.e. your class should implement IDisposable.



回答3:


If you really are maintaining a connection to a database (and its not just a sample code you wanted to post here), you are doing the right thing right now. That is opening database connection, querying for data and closing/disposing it.

On the other hand, IDisposable.Dispose() method should be used if your resources take up a lot of memory but are not time critical to be closed/disposed as soon as possible. When your object is no longer referenced and GC needs memory, your object will be collected and Dispose() method will be closed on it which will dispose your unmanaged large object.




回答4:


Yes, implementing IDisposable is how you tell clients of your code "hey, you need to call dispose on this object because it maintains references to unmanaged resources."



来源:https://stackoverflow.com/questions/4411917/disposing-of-unmanaged-objects-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!