S. ten Brinke
S. ten Brinke

Reputation: 3001

How to add all missing usings at once with Visual Studio 2019 and C#

When I write C#, I sometimes have 3-4 missing usings which I can add with the tooltip window -> add missing using, or doing ctrl + . On the line where a missing using is present.

Doing this 3-4 times is rather annoying.

I have noticed a "add all missing usings" tooltip sometimes, which worked, but I can not get it to pop up reliably.

I know resharper has it, but I don't want to use a paid extension for this feature.

How can I reliably get this "add all missing usings" pop up, or in what other way can I add all missing usings at once?

Thanks!

Upvotes: 16

Views: 6964

Answers (3)

Dirk Strauss
Dirk Strauss

Reputation: 641

Also, have a look at the free Auto-Using for C# Visual Studio Code extension. While I am not a huge fan of this, it might alleviate the pain of continually having to stop writing code to add a using statement. You can also view the project here on GitHub.

Personally, I just use Ctrl+. like you mentioned which fits in nicely with my way of writing code.

Upvotes: -2

A. Morel
A. Morel

Reputation: 10374

To complete @rhytonix answer... after pasting your code, you have to click on the screwdriver in the left margin like this:

enter image description here

Upvotes: 3

rhytonix
rhytonix

Reputation: 998

The "add missing usings" is triggered by a paste action. After you paste new code, the option will be available so long as you haven't made any edits to the document after pasting the code (making any edit removes the option). Also, the option doesn't add missing usings for the rest of the document -it only analyzes the pasted section-.

You can cut out your code and then paste it to trigger the action as unfortunately currently it is not part of the default Quick Actions refactorings.

Upvotes: 6

Related Questions