Subscribe to Subscribers

Resources » Code Snippets » C# Syntax

Using keyword


Posted Date:     Category: C# Syntax    
Author: Member Level: Gold    Points: 15


Defines a scope, outside of which an object or objects will be disposed The 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. This interface provides the Dispose method, which should release the object's resources..



Old code



SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(commandString, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();



using "using" keyword


using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(commandString, con))
{
con.Open();
cmd.ExecuteNonQuery();
}
}

C# will internally generate two try / finally blocks (one for the SqlConnection and one for the SqlCommand)

jeebu





Did you like this resource? Share it with your friends and show your love!


Responses to "Using keyword"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to remove folder security restrictions
    Previous Resource: C# Automatic Properties
    Return to Resources
    Post New Resource
    Category: C# Syntax


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Using  .  Dispose  .  IDisposible  .  

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.