18

I am using Visual Studio with Unity. When I copy and paste a code in Visual Studio. It automatically adds an unnecessary namespace on the top. It is pretty annoying to see if there have many unused namespaces over time. May I ask if is this a bug or how can I turn off the auto import namespace when copy paste?

From the suggested name, I think Visual Studio confuses the "length" and "Length" enter image description here

Here are the steps to reproduce the problem

enter image description here

Yiming
  • 321
  • 2
  • 10
  • Probably because the code doesnt compile and its trying to be helpful in a way only computers can. If you defined length and then hit paste does it quit being annoying? – BugFinder Oct 19 '22 at 16:12
  • @Yiming You should disable "Enable namespace update when moving files" in Visual Studio. But I can't find this option in the 2022 version! – Hossein Sabziani Oct 19 '22 at 16:18
  • Paste with the defined variable will not import the namespace. But I want to define the variable after work manually. There always have code with multiple variables that I cannot guarantee they all have defined. – Yiming Oct 19 '22 at 16:20
  • 1
    In the C# editor options, under Advanced, Using Directives, uncheck "Add missing using directives on paste". – madreflection Oct 19 '22 at 16:23
  • @madreflection it would be great if you made that comment into an answer! – TarHalda Oct 19 '22 at 17:30
  • C# editor checked "Add missing usage directives when pasting". as madreflection said – Housheng-MSFT Oct 20 '22 at 09:26

1 Answers1

21

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.

Might be useful to some to note that tab completion will still auto-add missing using directives.

enter image description here

Here is an example with the option checked (the example specifics are not relevant but in this case an attribute is being pasted to an ASP.Net Core Razor Page): Checked Option

Here is an example with the option unchecked: Unchecked option

keipala
  • 768
  • 2
  • 7
  • 19
  • Thanks! Just what I needed. Visual Studio has been auto-including the wrong choice of ambiguous classes too much on my latest project. – Davak72 Aug 02 '23 at 19:10