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#.

More Articles

 

Share your thought



PLEASE DISABLE ADBLOCKER AND SUPPORT US!

Please support us by enabling ads on this page. Refresh

YOU DON'T LIKE ADS, WE ALSO DON'T LIKE ADS!
 
But we have to show ads on our site to keep it free and updated. We have to pay huge server costs, domain costs, CDN Costs, Developer Costs, Electricity and Internet Bill. Your little contribution will encourage us to regularly update this site.