Tal Ramot
Tal Ramot

Reputation: 1

How to show all using directives - visual studio 2022

Thanks you for reading this! Is there an option in VS 2022 to show all the using directives at the top of the program? Because I have 2 libraries working with same names, I'm forced to type this long prefix (for the program to distinguish between the 2 libraries). Otherwise I get a CS0104 error . enter image description here

I figure that the recent version of VS 2022 doesn't simply show all using directives at the beginning of the program, so I couldn't find it anywhere.

Upvotes: 0

Views: 2407

Answers (1)

Christian.K
Christian.K

Reputation: 49260

That is because of global/implicit using. They are automatically generated by the build system.

Example:

global using global::System;

They could really be in an C# file of the project, but the default that the project system generates is in a file called <projectdir>\obj\<Release|Debug>\<tfm>\<projectname>.GlobalUsings.g.c. So in a project called ConsoleApp1.csproj, targeting .NET 6.0, in Release mode, the file is obj\Release\net6.0\ConsoleApp1.GlobalUsings.g.cs.

You can view the file using Windows Explorer, or show all files in Visual Studio.

Example:

For a standard console project, the content of the file is:

// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

Those are defined by MSBuild files, by the Using item group:

<ItemGroup>
   <Using Include="System.Collections.Generic"/>
   ...
</ItemGroup>

So you could add your own namespaces if you like.

To disable implicit global usings, that is the generation of the <projectname>.GlobalUsings.g.cs file, set the ImplicitUsings property to disable in your csproj file:

<PropertyGroup>
    <ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>

Make sure to rebuild (or clean) your project after that change, because the originally generated file would otherwise still linger around.

Upvotes: 1

Related Questions