C# 8 looks like a nice update. using
declarations in particular, though, are pretty darn cool.
I’m sure you’ve written a using within a using
within a using, right? Something like this:
SomeMethod()
{
using ( disposableObject1 )
{
using ( disposableObject2 )
{
using ( disposableObject3 )
{
// magic happens here
}
}
}
}
If you’re not intimately familiar with using
, when an object wrapped in a using
falls out of the using
scope, Dispose
will be called on it.
Multiple using
, like the above, certainly adds noise and can impede code legibility. It gets a bit better with stacking using
SomeMethod()
{
using ( disposableObject1 )
using ( disposableObject2 )
using ( disposableObject3 )
{
// magic happens here
}
}
C# 8 adds the nice feature of imlicit scope, so now, it looks like this:
SomeMethod()
{
using var disposableObject1 = new DisposableObject();
using var disposableObject2 = new DisposableObject();
using var disposableObject3 = new DisposableObject();
// magic happens here
}
Check it out here, or, look over at the Random Thoughts blog for more in-depth discussion.