I want to know which is the best way to use the 'using' block in C#.
Approach 1: Looping inside the 'using' block
void MyMethod(List<Prod> productList)
{
using(MyResource mr = new MyResource())
{
foreach(Prod product in productList)
{
//Do something with the resource
}
}
}
Approach 2: Looping outside the 'using' block
void MyMethod(List<Prod> productList)
{
foreach(Prod product in productList)
{
using(MyResource mr = new MyResource())
{
//Do something with the resource
}
}
}
I want to know which approach is preferable and why. Will there be performance differences between the two? Also how does it differ if the resource is... say a database connection or an object?
Thanks!