1

I have a C# 10 project with <ImplicitUsings> enabled:

    <LangVersion>10</LangVersion>
    <ImplicitUsings>enable</ImplicitUsings>

With this in place, VS will gray-out many common namespaces in code files and offer to remove them.

However, when I create a new C# file it still imports all of the now-unnecessary using statements by default:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyNamespace
{
    internal class Class1
    {
    }
}

Is this just a limitation of VS or is there something I can do to convince it to omit these namespaces from the new file template?

ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
  • 1
    I don't think you're going to be able to have it be "smart" about it and not include it because of your global usings. But you _could_ modify the existing template as described [here](https://stackoverflow.com/questions/8745688/how-can-i-change-the-default-visual-studio-c-sharp-new-class-file-template) – Kirk Woll Apr 20 '22 at 20:11
  • You can get rid of those unnecessary `using` statements on first save when you enable `Run Code Cleanup on Save` having `Remove unnecessary usings` included. – pfx Apr 20 '22 at 20:56
  • Or assign a keystroke to "Edit.RemoveAndSort" (I use ^U). Then you just have to press one key to remove the usings. – Matthew Watson Apr 20 '22 at 20:59
  • [I think you can create a new template with this tutorial.](https://learn.microsoft.com/en-us/dotnet/core/tutorials/cli-templates-create-item-template) – Jiale Xue - MSFT Apr 21 '22 at 03:09

1 Answers1

0

I think that the "ImplicitUsings" is not for what you look for. It only add some pre-defined usings to your project based on the project's SDK. See Implicit-using for reference.

But will not remove the usings directives of templates.