Dispose() is called automatically when the using block exits : IDisposable with using « Class « C# / CSharp Tutorial






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;

  } 
}
In Dispose()
In Dispose()








7.53.IDisposable with using
7.53.1.Dispose() is called automatically when the using block exits
7.53.2.Use Not Disposible object in using statement