Why Do We Use Using Keyword

Posted by Harsh_rc under C# on 8/26/2011 | Points: 10 | Views : 2040 | Status : [Member] | Replies : 4
whats the difference between all these statment and which one is much effecient.
=======================================
Customer cs=new Customer();
===========================================
using(Customer cs=new Customer())
{

}
==========================================
Customer cs=new Customer()
using(cs)
{

}




Responses

Posted by: Kishork80 on: 8/26/2011 [Member] Starter | Points: 25

Up
0
Down
using(Customer cs=new Customer())
{

}

is the short and performance wise better approach .
The reason being we are creating an object of Customer in one shot and using GC feature to dispose it using using.

---Kishor

kishor kumar

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Harsh_rc on: 8/26/2011 [Member] Starter | Points: 25

Up
0
Down
thanks for your replay but what if i have putted this code in method and i want to call this method many times.so in this case object is created every time and dispose,is it affect my performance.

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishork80 on: 8/26/2011 [Member] Starter | Points: 25

Up
0
Down
In all of your scenarios objects are created anayway , but the purpose of USING is to allow GC to take care of its dispose.and developer has not to worry about anything.

kishor kumar

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Harsh_rc on: 8/26/2011 [Member] Starter | Points: 25

Up
0
Down
so which one is better approach in my case.

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response