Combining foreach and using

foreach (ManagementObject result in results)
using(result)
{
    //code here
}

It’s not normally good practice to assign the variable outside the using block because the resource would be disposed but could stay in scope. It would, however, result in clearer code here because you can nested the using statement against the foreach.

EDIT:
As pointed out in another answer, ManagementObjectCollection also implements IDisposable so I have added that into a using block.

No need to place ManagementObjectCollection in a using statement. the foreach will call Dispose() on the enumerator.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)