Using statement in C#


The using statement defines the scope of the object/resource. When the scope of the using statement is (Implicitly i.e. when "}" of the using block is reached or explicitly i.e. by throwing exception from the using block) over then the resource is automatically disposed by the CLR rather by Garbage collector which is not determined. 

Use of using statement will affect the performance. As we store a large dataset and heavy object and if they are used with using statement then unnecessary resource will be released as soon as the using block is over.

A program/class that makes use of using statement must implement IDisposable interface. Following is the method provided by the IDisposable interface:

void Dispose();

To understand it I have defined a Movie class as shown below:

class Movie : IDisposable
{
    private String _name;
    private int _length;

    public String Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int Length
    {
        get { return _length; }
        set { _length = value; }
    }

    public void Display()
    {
        Console.WriteLine("Movie Name:{0}", Name);
        Console.WriteLine("Movie Length:{0}", Length);
    }

    void IDisposable.Dispose()
    {
        Console.WriteLine("Scope of using is over. The movie resource is disposed.");
    }
}

The Movie class implements the required interface. It has two data member Name and Length which stores the movie name and length of the movie respectively. The Display() method displays value of both the data member.

You will also notice that the Movie class also provide the definition for the dispose() of IDisposable interface. It contains a simple writeline statement which is used to notify us that the use of resource is over.

Now about the main program, the main program is shown below:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Before the using statement");
        using (Movie mv = new Movie())
        {
            Console.WriteLine("Inside the using statement");
            mv.Name = "Sholay";
            mv.Length = 204; // in minutes
            mv.Display();
        }
        Console.WriteLine("After the using statement");
    }
}

As shown above firstly main program display "Before the using statement" using writeline method. This method is just to notify that the next statement is Using statement. After the Using block main program display "After the using statement" using writeline method. This method is just to notify that the scope of the using statement is over.

Following is the sample output:

1.gif 

Let us elaborate the output:

Firstly the application prints "Before the using statement" when Console.WriteLine("Before the using statement"); ; line is encountered. Net the application prints "Inside the using statement" when Console.WriteLine("Inside the using statement"); line is encountered. Next the application assign the values and print them using the Display() method. When the scope of the using statement is over i.e. the "}" of using statement is reached CLR automatically call the Dispose() method of iDisposable interface implemented by Movie class. Lastly application prints "After the using statement" when Console.WriteLine("After the using statement"); line is encountered.

Up Next
    Ebook Download
    View all
    Learn
    View all