IDisposable的问题 [英] IDisposable Question

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

问题描述

说我有以下几点:

public abstract class ControlLimitBase : IDisposable 
{
}

public abstract class UpperAlarmLimit : ControlLimitBase 
{
}

public class CdsUpperAlarmLimit : UpperAlarmLimit 
{
}

两个问题:

1。
我就当我IDisposable的成员实际上被调用有点困惑。将用在CdsUpperAlarmLimit实例超出范围,他们被调用?

1. I'm a little confused on when my IDisposable members would actually get called. Would they get called when an instance of CdsUpperAlarmLimit goes out of scope?

2。
我将如何处理在CdsUpperAlarmLimit类创建的对象的处置?如果这也从IDisposable接口派生?

2. How would I handle disposing of objects created in the CdsUpperAlarmLimit class? Should this also derive from IDisposable?

推荐答案

的Dispose()从不自动调用 - 这取决于代码是如何实际使用

Dispose() is never called automatically - it depends on how the code is actually used.

1)的Dispose()时被调用。你特别呼吁的Dispose()

1.) Dispose() is called when you specifically call Dispose():

myAlarm.Dispose();



2)的Dispose()被称为在使用你的类型的实例的使用块的结尾。

2.) Dispose() is called at the end of a using block using an instance of your type.

using(var myAlarm = new CdsUpperAlarmLimit())
{

}



使用块是一个尝试/终于有一个呼叫阻塞<$ C $语法糖正在使用C>的Dispose()的对象在finally块。

The using block is syntactic sugar for a try/finally block with a call to Dispose() on the object "being used" in the finally block.

这篇关于IDisposable的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆