Using Keyword in C# Language

Introduction

Using keyword are use two main purpose in a c sharp program. First is,  you include namespace in your program, which is call to as a directive. Second is, you call to dispose function by the IDisposable objects, which is work as a try and finally statement. If you are declare any un-managed object in using block, then you don't need to explicitly dispose those object. Once the block execution is over, then Dispose will be called automatically.

Example

using System; 

using System.Text;

 

namespace using_keyword_in_c_sharp

{

    class test:IDisposable   //create class and release allocated resource

    {

        public void display()  //create a function

        {

            Console.WriteLine("Walcome");

        }

         void IDisposable.Dispose() //call to Dispose() function through IDisposable object

        {

            Console.WriteLine("Dispose");

            Console.ReadLine();

        }

    }

    class Program

    {

       

        static void Main(string[] args)

        {

            using (test t = new test())   //use using keyword

            {

                t.display();

            }

       

        }

    }

}

Output:

output.jpg

Ebook Download
View all
Learn
View all