Using declarations in C#8+ can allow us to dispose of resources correctly without increasing nesting levels

Since C#8 we can use single line using declarations which will allow us to dispose of resources at the end of the enclosing scope. This helps us reduce nesting in our code BUT…

… while the using {} approach allows us to explicitly tell the compiler when to dispose of a resource, the C#8+ using declaration approach only disposes resources at the end of the enclosing scope… this could in theory mean that resources are kept open longer than they need to be.

The examples below are stills I took from Microsofts C# Language Highlights: Using Declarations video on YouTube.


Need Help with Your C# Projects?

We offer expert support and development services for projects of any size. Contact us for a free consultation and see how we can help you succeed.

CONTACT US NOW