blog

Home / DeveloperSection / Blogs / Use of Using Keyword in C#

Use of Using Keyword in C#

AVADHESH PATEL4460 17-Jan-2013

We use “Using” keyword as directive in c#. “Using” keyword can be used for importing namespace to your classes.

Example:

Using System;

Using System.Data;

Another most useful advantage is it can be used in code blocks. It is called the “Dispose()” method automatically to release the resources and also automatically implements the try catch blocks  for you. Here it is called a using statement. So the advantage is that we don’t have to explicitly write a try-catch block and our code looks small.

Syntax:
using ()
{
}


Example: Defines a scope, outside of which an object or objects will be disposed.

The object can be declared in the using statement, as shown above, or before the using statement as below

Font font = new Font("Times New Roman", 12.0f);
using (font)
{
    // use font
}

Updated 18-Sep-2014
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By