Dispose() is called automatically when the using block exits : IDisposable with using : Class C# Examples


C# Examples » Class » IDisposable with using »

 

Dispose() is called automatically when the using block exits









    
using  System;
public  class  MyClass  :  IDisposable
{
    public  MyClass(){}

    public  void  Dispose()
    {
        Console.WriteLine("In  Dispose()");
    }
}

public  class  MainClass
{
    public  static  int  Main(string[]  args)
    {

        using(MyClass  c  =  new  MyClass())
        {
            
        }

        MyClass  c1  =  new  MyClass();
        c1.Dispose();            
        return  0;

    }  
}
    
   
  
   



Output

In Dispose()
In Dispose()


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» IDisposable with using