47

I don't mind the using directives which are automatically created when the script is created. Those are fine. What I'm talking about are the using directives which are automatically generated at the top of the script while I'm coding as soon as I refer to a type which is out of scope. A pox upon the silent fiends!

To clarify, currently if I try including a type which exists in some namespace which is not yet declared as 'using' in the script, said namespace will get auto-added to the top of the script. This is undesired behavior for me.

I simply prefer to manually add my namespaces.

8
  • 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). May 15, 2020 at 4:41
  • 2
    Also the "right" terminology here is, using directive
    – TheGeneral
    May 15, 2020 at 6:39
  • 1
    I ruled out those extensions as the problem. Disabling them did not stop the automatic generation behavior for using directives.
    – Nate H.
    May 15, 2020 at 20:15
  • 1
    Also, I've verified that the automatic generation behavior happens even in non-Unity projects. For example, it will occur in a console app project.
    – Nate H.
    May 15, 2020 at 20:38
  • 21
    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.
    – MGOwen
    Dec 6, 2022 at 1:39

4 Answers 4

38

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

1
  • 3
    The location has changed (Feb 2023), but searching for "Add missing using directives on paste" will let you find it quickly.
    – Nandostyle
    Feb 4 at 16:01
16

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

4
  • 4
    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.
    – Paul B.
    Nov 12, 2020 at 15:09
  • 1
    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
    Dec 20, 2022 at 22:15
  • @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]
    – PatrickV
    Mar 2 at 2:30
  • 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.
    – BDisp
    Aug 2 at 20:50
10

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

6

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.