I'm iterating over a ManagementObjectCollection ( which is part of the WMI interface).
However, the important thing is, the following line of code. :
foreach (ManagementObject result in results)
{
//code here
}
The point is that ManagementObject also implements IDisposable, so I would like to put the "result" variable in a using block. Any idea on how to do this, without getting too weird or complex?
foreach (ManagementObject result in results) using (result)
{
//code here
}
Assigning the variable outside the using
block is not normally good practice because the resource would be disposed of but could stay in scope. It would, however, result in clearer code here because you can nest the using
statement against the foreach
.