Home > Back-end >  global usings and .NET Standard 2.0
global usings and .NET Standard 2.0

Time:03-02

I recently recognized that I can use the C# 10 feature file-scoped namespaces in .NET Standard 2.0 projects as well by setting <LangVersion>10</LangVersion> in the csproj file.

However, global usings don't work that way, I'm getting compiler errors due to missing using statements.

Are there any tweaks so that I can use global usings in a .NET Standard 2.0 library as well?

CodePudding user response:

I'm not sure why it doesn't work with a separated .cs file. However, a workaround that works is using the MSBuild syntax. In your .csproj you can add the following:

  <ItemGroup>
    <Using Include="System.Linq" />
  </ItemGroup>

There are some keywords you can use - like Alias or Static -, as you would do in a normal .cs file.

  <ItemGroup>
    <Using Include="Test.Namespace" Alias="Domain" />
  </ItemGroup>

And then in your code, you can do the following:

namespace Test.Namespace 
{
    public class TestClass {}
}

namespace Another.Namespace
{
    new Domain.TestClass();
}

If it helps, I found this information in the following blog post.

  • Related