Namespace ‘Hacks’ in Project Files
In this post, I wanted to cover a few tricks you can use to fine-tune how your C# projects deal with namespaces. I’ll show examples of the project file open in a text editor, rather than in the GUI editor in Visual Studio, but you can open any project file in a text editor by right-clicking on the project in the Solution Explorer and choosing Edit Project File.
By default, the things in your project should match your project name, with folders adding additional elements to the namespace name.
So if I have a project named
CoolProject, then my namespace should typically be
CoolProject as well.
If there’s a folder called
UI inside of that project, then things within that folder should typically be in the
Visual Studio and other editors will help enforce this convention, and can also give you tools to move a class to the correct namespace to reflect that structure.
If you follow the convention, you won’t need to do any special project configuration.
But if you want a project to use a different namespace, then that can be done by adding another item in the
<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <RootNamespace>CompanyInc.CoolProject</RootNamespace> <!-- THIS LINE --> </PropertyGroup>
First off, there’s the option to enable or disable the entire feature of implicit using directives (the automatic inclusion of
System.Linq, etc.) by adding this item to your project’s
In the context of the file, it usually looks something like this:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <!-- THIS LINE --> <Nullable>enable</Nullable> </PropertyGroup> </Project>
The default is
disable, so you can turn this feature off for a project by either setting this to
disable instead of
enable or by simply removing the line.
I don’t recommend that. It’s a feature that is way too convenient to pass up.
It allows your block of
using directives to contain only the non-obvious ones, shedding the obvious ones like
using System; and
But old-school C# code demanded that, and if you’re a glutton for punishment and want to Stay Classic, then you can go back to the old way.
You may want to leave the implicit using directives on, but want to tweak the list of namespaces that are automatically included. For example,
System.Collections.Generic is in the list to be included.
If you wanted to exclude just that one item, you can do that by adding in something like this:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> ... </PropertyGroup> <ItemGroup> <Using Remove="System.Collections.Generic"/> </ItemGroup>
Remove in there, now
System.Collections.Generic will no longer be one of the automatically included
You can do a similar thing to add additional
using directives to a project.
This accomplishes the same thing as a plain old
global using, just done differently:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> ... </PropertyGroup> <ItemGroup> <Using Include="System.Text"/> </ItemGroup>