本文介绍了功能级别的IDisposable接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在函数级别创建的对象很少,即在类中的函数本地创建的对象.

我想使用IDisposable接口来放置对象.有人可以帮我如何实现这一目标.

提前谢谢!

谢谢
Govardhan

Hi,

I have few objects create at function level, i.e., local to a function in a class.

I want to use the IDisposable interface to dispose the objects. Can someone help me how this can be implemented.

Thanks in advance!

Thanks
Govardhan

推荐答案

try
{
    using(MyDisposableObject obj = new MyDisposableObject())
    {
    //code here..
    }
}
catch(Exception ex)
{
//error handling here..
}



这将在对象超出范围时立即对其进行处理,并确保在存在异常的情况下对其进行正确处理.



this will dispose of the objects as soon as they are out of scope and will also ensure they are correctly disposed of if there''s an exception.


这篇关于功能级别的IDisposable接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 18:37