Stop VS from automatically adding using directives
Asked Answered
W

4

61

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.

Wary answered 15/5, 2020 at 4:34 Comment(8)
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).Airman
Also the "right" terminology here is, using directiveDreibund
Can you please explain what you mean with "script"? What type of script, what language?Paginate
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.Wary
I ruled out those extensions as the problem. Disabling them did not stop the automatic generation behavior for using directives.Wary
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.Wary
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.Diao
Since this was the top result on google, here's the answer: #74129006Recurrence
R
58

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

Recurrence answered 17/1, 2023 at 22:57 Comment(2)
The location has changed (Feb 2023), but searching for "Add missing using directives on paste" will let you find it quickly.Damle
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.Wimsatt
G
22

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

Gallice answered 23/5, 2023 at 13:43 Comment(2)
What if I'm not pasting but typing?Wimsatt
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?Radicalism
W
17

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

Wary answered 21/7, 2020 at 19:34 Comment(5)
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.Photomechanical
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.Metabolic
@Metabolic 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]Precondemn
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.Phraseograph
@Precondemn I have this option turned off and it still inserts usings everywhereDiameter
A
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

Abagael answered 8/9, 2022 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.