40

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Nate H.
  • 563
  • 5
  • 11
  • 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`). – John Alexiou May 15 '20 at 04:41
  • 1
    Also the "right" terminology here is, [*using directive*](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive) – TheGeneral May 15 '20 at 06:39
  • Can you please explain what you mean with "script"? What type of script, what language? – Heinz Kessler May 15 '20 at 07:12
  • Perhaps it's due to an extension. I don't have many, but the ones which seem like potential culprits are 'Visual Studio 2019 Tools for Unity' and 'Visual Studio IntelliCode'.. I'm using Unity and Visual Studio Community 2019 version 16.5.4. By "script", I mean a C# script in a Unity project. – Nate H. May 15 '20 at 20:08
  • 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 '20 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 '20 at 20:38
  • 13
    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 06 '22 at 01:39
  • 1
    Since this was the top result on google, here's the answer: https://stackoverflow.com/questions/74128506/prevent-visual-studio-from-auto-import-namespace-when-copy-paste – MastaKwaa Jan 17 '23 at 22:48

4 Answers4

34

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

MastaKwaa
  • 341
  • 2
  • 3
  • 3
    The location has changed (Feb 2023), but searching for "Add missing using directives on paste" will let you find it quickly. – Nandostyle Feb 04 '23 at 16:01
15

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

Nate H.
  • 563
  • 5
  • 11
  • 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 '20 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 '22 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 02 '23 at 02: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 02 '23 at 20:50
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

Shreedhar Kotekar
  • 1,034
  • 10
  • 20
4

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

Roshith S
  • 101
  • 6