C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Using Statement in C#


Posted Date: 21 Aug 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Nagarajan       Member Level: Gold
Rating:     Points: 10



IDisposable Interface

Before looking into using statement, we need to have a look at IDisposable interface.

IDisposable interface (when implemented) signals users of this class that it offers a public method (Dispose()) that can be called to deterministically close the resource.

IDisposable Definition:
public interface IDisposable {
void Dispose();
}


When your class implements the object's Dispose or Close method, it's highly recommended to keep then in the finally block, so it's guaranteed that the dispose or close method called


public static void Main()
{
try
{
SQLConnection connection = new SQLConnection(ConnstringString);
...
}
finally
{
if (connection != null)
{
// better to check for the connection state before closing it
connection.close();
connection.dispose();
}
}
}


Using Statement in C#

Adding an exception handling code is always the best thing to do. Fortunately C# has given us the using statement, which offers an identical alternate to the above. So rewriting the above code.

public static void Main()
{
using (SQLConnection connection = new SQLConnection(ConnstringString))
{
...
}
}


In the using statement, we actually initialize an object and save its reference in a variable or use a preintializ. Then we access the variable via code contained inside using's braces.
When this code gets compiled, the compiler automatically emits the try and finally blocks (No Catch block is included).

The object intialized above need to be an IDisposable implementation, because inside the generated finally block, the compiler emits code to cast the object to an IDisposable and calls the Dispose method.
So it's obvious, that the compiler allows the using statement to be used only with types that implement the IDisposable interface.

Also Inside the using statement we can intialize multiple variables, provided all the variables of same type.

Happy Coding !!!




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Using  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: .NET Assembly
Previous Resource: Grouping using Gridview
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

internet fax

Contact Us    Privacy Policy    Terms Of Use