-
Notifications
You must be signed in to change notification settings - Fork 224
File‐Scoped Namespaces
C# 10 (released with .NET 6 in 2021) introduced file-scoped namespaces. They are a shorter way to declare a namespace that applies to the entire file, removing the need for the surrounding curly braces and reducing indentation by one level.
Traditional block-scoped namespace (C# 1–9):
namespace MyApp.Models
{
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
}File-scoped namespace (C# 10+):
namespace MyApp.Models;
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}The two are functionally identical — the only difference is syntax and indentation. File-scoped namespaces are now the default style in new .NET project templates and are preferred by most modern C# style guides.
Note: File-scoped namespaces require C# 10 or later, which means your project must target .NET 6 or later (or set
<LangVersion>10</LangVersion>or higher in your.csproj). If you are on EF6 or an older .NET target, leave this setting atfalse.
Open your Database.tt file and find the Code suppression section. Change UseFileScopedNamespaces from false to true:
// Code suppression ****************************************
Settings.UseRegions = true;
Settings.UseNamespace = true;
Settings.UseFileScopedNamespaces = true; // <-- change this
Settings.UsePragma = false;Save the file. The generator will re-run automatically (this is how T4 templates work in Visual Studio — saving the .tt file triggers generation). All generated .cs files will now use file-scoped namespace syntax.
Before (UseFileScopedNamespaces = false):
// <auto-generated>
namespace MyApp.Models
{
public class Customer
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
// Reverse navigation
public virtual ICollection<Order> Orders { get; set; }
}
}
// </auto-generated>After (UseFileScopedNamespaces = true):
// <auto-generated>
namespace MyApp.Models;
public class Customer
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
// Reverse navigation
public virtual ICollection<Order> Orders { get; set; }
}
// </auto-generated>Before:
// <auto-generated>
namespace MyApp.Models
{
public class MyDbContext : DbContext, IMyDbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public MyDbContext()
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new CustomerConfiguration());
modelBuilder.ApplyConfiguration(new OrderConfiguration());
}
}
}
// </auto-generated>After:
// <auto-generated>
namespace MyApp.Models;
public class MyDbContext : DbContext, IMyDbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public MyDbContext()
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new CustomerConfiguration());
modelBuilder.ApplyConfiguration(new OrderConfiguration());
}
}
// </auto-generated>| Benefit | Explanation |
|---|---|
| Less indentation | All class members sit at column 0 instead of being indented inside the namespace block. This is one less level of nesting throughout every file. |
| Fewer lines | No opening or closing brace for the namespace, reducing noise. |
| Consistency with .NET templates | New projects created with dotnet new use file-scoped namespaces by default since .NET 6. |
| Cleaner diffs | Removing an indentation level means version control diffs are less noisy when class members change. |
Q: Do I need to change anything else in my project?
No. The setting only changes how the namespace declaration is written. Your using statements, class names, and everything else stay the same.
Q: Can I mix file-scoped and block-scoped namespaces in the same project?
Technically yes — C# allows different files to use different styles. However, it is bad practice to mix them. If you enable this setting, all generated files will use file-scoped style. You should also update any hand-written files in the same project to match, or configure a .editorconfig rule to enforce consistency.
Q: My project targets .NET 4.8 / EF6. Can I use this?
No. File-scoped namespaces require C# 10, which requires the .NET 6 SDK or a <LangVersion>10</LangVersion> override. If you are generating EF6 code for a .NET Framework project, leave UseFileScopedNamespaces = false.
Q: The generated code looks wrong — classes appear outside the namespace.
This most likely means your project is targeting a C# version older than 10. Check your .csproj for:
<LangVersion>latest</LangVersion>or ensure your <TargetFramework> is net6.0 or higher. If you cannot update the language version, set UseFileScopedNamespaces = false.
Q: What happens if UseNamespace is false?
UseFileScopedNamespaces has no effect when UseNamespace = false. If namespace generation is suppressed entirely, no namespace declaration is written regardless of the file-scoped setting.
| Setting | Type | Default | Description |
|---|---|---|---|
UseNamespace |
bool |
true |
Controls whether any namespace declaration is written at all. |
UseFileScopedNamespaces |
bool |
false |
When true and UseNamespace is also true, emits namespace X; instead of namespace X { }. Requires C# 10+. |