C# 10.0 Introducing File Scoped Namespaces

C# 10.0 Introducing File Scoped Namespaces

Microsoft released .NET 6 Preview 7 SDK last week with File Scoped Namespaces feature. In this article, you will learn File Scoped Namespaces. You can use this feature only with the latest Visual Studio 2022 Preview and that includes the latest .NET 6.0 preview SDK. Also, you can install .NET 6 Preview 7 SDK to use with the latest Visual Studio Code.


C# 10.0 Introducing File Scoped Namespaces

In the below example, you can see a c# sample code that shows the traditional way of namespace declaration in the earlier version of C# (before C# 10.0). Here there is no issue except additional indentation with open ‘{‘ and close ‘}’ curly braces.

.
/*
 * Namespace declaration prior to C#10.0
 */
namespace MyProject.Accounts
{
    public class AccountManager
    {
    }
}

In the C# 10.0 version, the File Scoped Namespaces feature simplified the namespace declaration where additional indentation has been removed. The new code looks cleaner as below.

.
/*
 * Namespace declaration from C#10.0 onwards.
 */

namespace MyProject.Accounts;

public class AccountManager
{
}

This feature reduces one level of additional indentation, and you can treat this as some readability improvements.



– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Recommended:   [C# Code Sample] C# Program to Create File
Share this:
We will be happy to hear your thoughts

Leave a reply

Troubleshootyourself
Logo