IDisposable Question

前端 未结 8 1649
野的像风
野的像风 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:23

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

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

    myAlarm.Dispose();
    

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

    using(var myAlarm = new CdsUpperAlarmLimit())
    {
    
    }
    

    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.

提交回复
热议问题