Prevent Visual Studio from auto import namespace when copy paste
Asked Answered
J

1

25

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

Jadwigajae answered 19/10, 2022 at 16:4 Comment(6)
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?Absolve
@Jadwigajae You should disable "Enable namespace update when moving files" in Visual Studio. But I can't find this option in the 2022 version!Kurdistan
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.Jadwigajae
In the C# editor options, under Advanced, Using Directives, uncheck "Add missing using directives on paste".Gonsalez
@Gonsalez it would be great if you made that comment into an answer!Displayed
C# editor checked "Add missing usage directives when pasting". as madreflection saidSetter
D
33

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

Discommode answered 22/10, 2022 at 12:27 Comment(1)
Thanks! Just what I needed. Visual Studio has been auto-including the wrong choice of ambiguous classes too much on my latest project.Lysine

© 2022 - 2024 — McMap. All rights reserved.