Removed nesting levels through block-scoped using
s
#90
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
.NET allows to initialize and properly dispose of
IDisposable
objects (usually objectd that handle external system resources whose structure is foreign to the runtime and have to be disposed of explicitly) throughusing
statements. This means that they will be disposed of when exiting the block, regardless of how it happens (be it through normal control flow or an exception being thrown).Formerly, the best, cleanest code we were able to write was something like this:
In newer versions of .NET, though, we can do the following:
This looks closer to regular variable initialization, which allows us to keep the statement more readable, whist preserving the benefits of handling our
IDisposable
objects properly.Changes proposed in this pull request
In this pull request, I've aimed to improve code readability by translating
using
statements from the former syntax to the newer syntax in several places throughout the code. This has helped me reduce block nesting levels, which are one of the things that can make it harder to understand what is happening.