85

What can I do to prevent Visual Studio from automatically adding using directives to the top of my source file?

I don't mind the using directives which are automatically created when Visual Studio creates a new source file. Those are fine.

What I'm talking about are the using directives that are automatically generated at the top of the source file while I'm coding. As soon as I refer to a type which is out of scope, Visual Studio silently adds new using directives.

To clarify, currently if I try including a type which exists in some namespace that is not yet declared as 'using' in the source file, Visual Studio will auto-add that namespace to the top of the source file.

This is undesired behavior for me. I simply prefer to manually add my namespaces.

10
  • 3
    No it does not. If I type something not in scope I get a little light buld which allows be to add the missing using (such as using System.Linq;) or to use the full type (such as System.Linq.Enumerable). Commented May 15, 2020 at 4:41
  • 2
    Also the "right" terminology here is, using directive Commented May 15, 2020 at 6:39
  • 1
    Can you please explain what you mean with "script"? What type of script, what language? Commented May 15, 2020 at 7:12
  • 41
    This feature has, multiple times now, silently added in some using directives from weird namespaces, causing me obscure nonsensical build errors. It takes me a while to track down what happened. That's so much worse than having to right click and add one. I think we can safely conclude that this "experimental feature" is a dud, and should be turned off by default in the next update of Visual Studio. Commented Dec 6, 2022 at 1:39
  • 2
    Since this was the top result on google, here's the answer: stackoverflow.com/questions/74128506/… Commented Jan 17, 2023 at 22:48

4 Answers 4

79

On the top of the page in Visual Studio, go to Tools > Options > Text Editor > C# > Advanced > Uncheck the option for "Add missing using directives on paste" then click OK on the bottom of the page. And then restart Visual Studio.

Answer was originally posted (much better) here, but posted here since this is the top google result and didn't have the answer:

Prevent Visual Studio from auto import namespace when copy paste

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

3 Comments

The location has changed (Feb 2023), but searching for "Add missing using directives on paste" will let you find it quickly.
Original poster's question does not refer to Copy/Paste and this answer only pertains to that situation. What happens when I type the missing the namespace instead of pasting it? Answer: The using statement is automatically added.
That solution helps with copy-paste but it does not help with auto-complete (when you hit enter at a suggested item from Intellisense dropdown while typing)
27

In Visual Studio 2022, The option to stop VS from automatically importing the directories on paste has been moved under tools -> options -> text editor -> C# -> advanced. From here you could deselect the option. It is selected by default. Quite annoying ! Posting screenshot for reference : enter image description here

3 Comments

What if I'm not pasting but typing?
How about when await is added inside a non-async method? VS is automatically adding the async modifier to the method. How to disable this?
Although there is another answer with same solution, voting this one up because it has the screenshot.
21

Under Tools > Options... > Text Editor > C# > IntelliSense, uncheck 'Show items from unimported namespaces (experimental)'.

5 Comments

Thanks for sharing the solution. I only wish Microsoft would add an option to still show results from unimported namespaces but add the namespace when autocompleting instead of a using.
Didn't work for me as of with Version 17.4.3. It still adds using when i copy/paste code. Such a shame and unprofessional of MSFT.
@edwabr123 there is a separate feature for doing this when pasting in Visual Studio 17.4.4 [Tools->Options->Text Editor->C#->Advanced->Add missing using directives on paste]
This is right answer for non the pasting cases, but it's needed to restart VS after changing it to take effect. Thanks @nate-h.
@PatrickV I have this option turned off and it still inserts usings everywhere
7

Even I find this annoying. I was able to turn this off in VS 2022 using the following options. Hope this helps some one.

I do agree that Microsoft should give us an option to list the items from unimported namespaces but not add using statements automatically.

enter image description here

Comments

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.