C# 10.0: File Scoped Namespaces – Get More Space For Your Code

C# 10.0 and .NET 6.0 will be released in November 2021. Time to look at the new language features with a little series of blog posts. Let’s start in this blog post with a very simple feature that is called File Scoped Namespaces.

First, let’s look at the normal namespace declaration that is part of C# since version 1.0.

The Normal Namespace Declaration

Let’s say you created an Employee.cs file with the following content. As you can see, the Employee class is in the namespace EmployeeManager.Model.

using System;

namespace EmployeeManager.Model
{
  public class Employee
  {
    public string? FirstName { get; set; }
    public DateTime? Birthday { get; set; }
  }
}

When you look at the code snippet above, you can see that the normal namespace declaration needs a pair of curlies, and the class inside the namespace is indented, which means you lose a bit of horizontal and vertical space.

With C# 10.0, you can address this little issue by declaring the namespace as a File Scoped Namespace.

The File Scoped Namespace Declaration

The following snippet shows a file-scoped namespace declaration that is possible with C# 10. As you can see, after the namespace you add a semicolon, and not an opening curly like with a classic namespace declaration. This file-scoped namespace means that all types in this file, like classes and interfaces, are in the namespace EmployeeManager.Model.

using System;

namespace EmployeeManager.Model;

public class Employee
{
  public string? FirstName { get; set; }
  public DateTime? Birthday { get; set; }
}

Of course you can also put the namespace declaration at the top of the file before the using directives. Especially if you have multiple using directives, you might prefer it this way. I think I’ll always put it at the top:

namespace EmployeeManager.Model;

using System;

public class Employee
{
  public string? FirstName { get; set; }
  public DateTime? Birthday { get; set; }
}

It’s not possible though to put the namespace declaration at the bottom of the file. As you can see in the following screenshot, it must precede all other members in a file. Only using statements are allowed before the file-scoped namespace.

Only One File Scoped Namespace Per File

You can declare only one file-scoped namespace per file. Usually, most C# files have only one namespace, so that’s totally fine. Take the Employee.cs file from the previous section as an example. There is only a single namespace, which means you can use a file-scoped namespace to make your C# code more compact and more readable.

But what if you want to declare more than one namespace in a single file?

That’s not possible with file-scoped namespaces. Let’s say you want to add a Developer class to the same file, but to a different namespace. When you try to add another file-scoped namespace in the same file like in the screenshot below, you get the error that says that a Source File can only contain one file-scoped namespace declaration.

It is also not possible to mix both namespace declaration styles in a single file. If you try to use a normal namespace declaration for the second namespace like in the screenshot below, you get an error that says that a Source file can not contain both file-scoped and normal namespace declarations.

This means, if you have a single namespace in your file, you can use a file-scoped namespace declaration, but if you want to use more than one namespace per file, you have to go with normal namespace declarations. The error from the screenshot above can be solved by using two normal namespace declarations like this:

using System;

namespace EmployeeManager.Model
{
  public class Employee
  {
    public string? FirstName { get; set; }
    public DateTime? Birthday { get; set; }
  }
}

namespace EmployeeManager.Model.Special
{
  public class Developer : Employee
  {

  }
}

If you’re a fan of nesting namespaces in a single file, you can also put the Developer class into the EmployeeManager.Model.Special namespace with a nested namespace declaration like in the code snippet below (I’m not a fan of nesting namespaces like below, because I think the code snippet above is easier to read than the code snippet below, but they are leading to the same result and C# allows you to do it either way).

using System;

namespace EmployeeManager.Model
{
  public class Employee
  {
    public string? FirstName { get; set; }
    public DateTime? Birthday { get; set; }
  }

  namespace Special
  {
    public class Developer : Employee
    {

    }
  }
}

Summary and Thoughts

File-scoped namespaces are a little, but useful extension to the C# programming language. I really like them, and I will definitely use them in my programs. I think I will put the file-scoped namespace declarations as first statements into my C# files.

I hope you enjoyed reading this blog post. In the next blog post, you will learn about another feature of C# 10, which is called Global Using Directives.

Share this post

Comments (3)

  • Michiel Doeven Reply

    Yeah, I like the change in namespaces as well, also the global using of namespace.

    September 21, 2021 at 11:06 pm
  • Jayakumar Vinayagam Reply

    Am not big fan of this feature, but it good to use to make more readable.
    I hope the same design work for partial class too?

    September 28, 2021 at 9:04 pm
    • Thomas Claudius Huber Reply

      Yes, sure, it works for partial classes too. No matter what type you define, class, interface, delegate etc.

      September 29, 2021 at 11:40 am

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.