Use file-level namespaces in C# 10 to reduce vertical nesting

In C# 9 we saw how top-level statements can help us reduce ‘wasted’ vertical space…


BUT… reducing ‘wasted’ horizontal space and in particular nesting levels is much more valuable IMHO and in C# 10 (released November 2021) we have file-level namespaces to help with this.

File-level namespaces are definitely one of my favourite features from C# 10 and Milan Jovanović’s example (originally posted on LinkedIn) below shows what they look like…

Another approach to reducing nesting levels is to use guard clauses.

Leave a Reply

Your email address will not be published. Required fields are marked *