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

Use of Using Keyword in C#


C# C# 
Ratings:
0 Comment(s)
 1838  View(s)
Rate this:

Use of Using Keyword in C#

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

}


Don't want to miss updates? Please click the below button!

Follow MindStick