Tuesday, July 24, 2007

using Keyword: Two uses of using keyword

using: Keyword
• Defines a scope, outside of which an object/objects will be disposed
• using is also used to specify the namespace like: using System.Xml
• using statement allows the programmer to specify when objects that use resources should release them
• The object provided to the using statement must implement the Idisposable interface
• Idisposable interface provides Dispose method, which should release object’s resources
• Example:
using (SqlConnection connection = new SqlConnection(connectionString))
{
// using ensures connection object is disposed when control exits using
//block
}

No comments: