When to dispose and why?

后端 未结 12 1064
臣服心动
臣服心动 2020-12-10 14:20

I asked a question about this method:

// Save an object out to the disk
public static void SerializeObject(this T toSerialize, String filename)
{
           


        
12条回答
  •  臣服心动
    2020-12-10 14:38

    If you are opening a resource (such as a file, or opening a database connection) then disposing the resource will release its hold on the resource. If you don't do this then other people might not be able to connect to the database or use the file.

    As a general rule of thumb....if the class implements the IDisposable interface, then you should call the Dispose() method when you are finishing it. More than likely there was a reason for them making it disposable :)

提交回复
热议问题