2
Answers

NP113 using keyword

Maha

Maha

16y
2.3k
1

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)
Next Recommended Forum