IDisposable Question

前端 未结 8 1661
野的像风
野的像风 2021-01-11 18:35

Say I have the following:

public abstract class ControlLimitBase : IDisposable 
{
}

public abstract class UpperAlarmLimit : ControlLimitBase 
{
}

public cl         


        
8条回答
  •  北荒
    北荒 (楼主)
    2021-01-11 19:28

    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?

    No. Its get called when you use using construct as:

    using(var inst = new CdsUpperAlarmLimit())
    {
        //...
    }//<-------- here inst.Dispose() gets called.
    

    But it doesn't get called if you write this:

    {
       var inst = new CdsUpperAlarmLimit();
       //...
    }//<-------- here inst.Dispose() does NOT get called.
    

    However, you can write this as well:

    var inst = new CdsUpperAlarmLimit();
    using( inst )
    {
        //...
    }//<-------- here inst.Dispose() gets called.
    

提交回复
热议问题