Using File-Scoped Namespaces in .NET 6

A subtle change which can really help with the clarity of your code

Jamie Burns
4 min readMay 24, 2022
Photo by Johannes Plenio on Unsplash

With the release of C# 10 and .NET 6, there have been a few new features that are simple but incredibly useful.

My favourite so far is File-Scoped Namespaces, which we’ll go through in this article.

What are File-Scoped Namespaces?

As its name implies, a file-scoped namespace is a namespace that has the whole file as its scope.

Let’s look at an example.

A traditional way to define a class in a namespace is something like this:

Here we can see that we’re defining the Reader class within the Logic.IO namespace.

However, in .NET 6/C# 10 we can update this to use a file-scoped namespace:

--

--

Jamie Burns
Jamie Burns

Written by Jamie Burns

Software Engineer, founder of Bungalow64 Technologies