C# Using Statement

In this chapter you will learn:
  • What is Using Statement in C#?
  • What is the functionality of Using Statement in C#?
  • How to use Using Statement in C# programming?

The using statement is mostly used when you need to one or more resources in a segment. The using statement obtains one or various resources, executes them and then releases the objects or resources. It is widely used in database connectivity through C#.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Using_Statement
{
    class check_using : IDisposable
    {
        public void Dispose()
        {
            Console.WriteLine("Execute  Second");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (check_using c = new check_using())
            {
                Console.WriteLine("Executes First");
            }
            Console.WriteLine("Execute Third");
            Console.ReadLine();
        }
    }
}

 

In the above example, the using statement calling the check_using class to execute. So, it first executes the using block, then executes the check_using class and finally executes the last statements.

Output

Executes First
Execute Second
Execute Third
__

Summary

In this chapter you learned what Using Statement is and how to use it in C# programming. In next chapter you will learn about Enumeration in C#.

 

Share your thought