Maha

Maha

  • NA
  • 0
  • 324.8k

NP113 using keyword

Aug 5 2008 5:31 PM

Hi Guys

 

NP113 using keyword

 

In the following program there is a using keyword (highlighted is yellow) which is new to me. Please explain the function of using keyword.

 

Thank you

 

using System;

using System.Collections.Generic;

using System.Reflection;

using System.Runtime.InteropServices;

 

class MyClass : IDisposable

{

    public event EventHandler OnInitialized;

    public event EventHandler OnDisposed;

 

    public void Init()

    {

        // Initialize our state...

        EventHandler onInit = OnInitialized;

        if (onInit != null)

            onInit(this, new EventArgs());

    }

 

    public void Dispose()

    {

        // Release our state...

        EventHandler onDisp = OnDisposed;

        if (onDisp != null)

            onDisp(this, new EventArgs());

    }

}

 

public class MainClass

{

    public static void Main()

    {

        using (MyClass f = new MyClass())

        {

            f.OnInitialized += delegate { Console.WriteLine("init"); };

            f.OnDisposed += delegate { Console.WriteLine("disposed"); };

            f.Init();

        }

    }

}

/*

init

disposed

*/


Answers (2)