2

Implicit usings is nice as it removes a lot of boilerplate, but sometimes I catch myself wanting to easily see which dependencies a specific file has. Not even after decompiling the dll the implicit usings are shown.

Is there any neat way to check this?

Now, I do one of these, not neat ways:

  1. Hovering over each class
  2. Turning off implicit usings and import all missing usings, and then revert

3 Answers 3

1

This doesn't cover global using ...; statements in .cs files, but for a fairly simple approach to just get some output related to the <Using Include="..." /> elements provided via MSBuild (project SDK, NuGet packages, Directory.Build.props, .csproj, etc.) you can add this to a project :

  <Target Name="ListUsings" AfterTargets="Build">
    <Message Importance="high" Text="@(Using)" />
  </Target>

With .NET 10 Preview the default list I got in an empty project was:

System
System.Collections.Generic
System.IO
System.Linq
System.Net.Http
System.Threading
System.Threading.Tasks

Though if you use a .NET Framework target framework (e.g. net48), starting with the .NET 8 SDK System.Net.Http is no longer included because it is not automatically available in the base framework and requires a NuGet Package.

Sign up to request clarification or add additional context in comments.

Comments

0

For decompiling I usually use dotPeek, which doesn't show the usings (might be some setting I'm not aware of).

I googled for alternatives and tried out ILSpy VS extension which actually does the job!

I don't mark this as correct answer yet, if anyone knows a way that doesn't require external tools, that would be preferable.

Gif showing how usings can be shown

Comments

0

Visual Studio shows a "global 'using' directive" icon in the top-left corner of the editor:

enter image description here

If you click it, it will reveal your global usings. This includes the ones from the project template (*.GlobalUsings.g.cs) as well as your explicitly declared global usings.

This does, however, not show the dependencies, as always all the global usings will be listed. But still a nice feature.

It helps to set the following Visual Studio Option:

enter image description here

You can then simply click on a directive in the pop-up shown in the first image to open the corresponding global usings file and to see the faded out directives.

Btw., I wonder why they have chosen to include System.Net.Http in a WinForms project (shown in the image above).

2 Comments

Thanks, didn't know about that. Didn't answer the question though, and as I tend to keep global usings in a separate file, this doesn't add much value. Good question about System.Net.Http, according to this link it's removed, a bit vague what they're referring to though, learn.microsoft.com/en-us/dotnet/core/project-sdk/….
Comment after edit: I'd like to see the usings for a specific file, not which usings global usings that are unused for the whole project, see the gif below in my answer, where I can check the usings that applies for a specific file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.