10

When using ReSharper to move/update namespace declarations, is there a way to stop ReSharper from removing unused Using statements?

In other words, if I have a class such as:

using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace Foo.Bar
{
    class MyClass
    {
        List<string> Names { get; set; }
    }
}

And I want to move it into the Foo.Bar.Utilities namespace using ReSharper, Resharper will remove all the unused Using statements and leave me with:

using System.Collections.Generic;

namespace Foo.Bar.Utilities
{
    class MyClass
    {
        List<string> Names { get; set; }
    }
}

However, I do not want ReSharper to touch my Using statements while moving the namespace declaration. I'd prefer to have the result as:

using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace Foo.Bar.Utilities
{
    class MyClass
    {
        List<string> Names { get; set; }
    }
}
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
  • Can I ask why this does not agree with you? – Rippo Jan 24 '11 at 16:58
  • 6
    If they are unused, why are you finding it to be such a problem? – Oded Jan 24 '11 at 17:00
  • Often times I'll bring in my namespaces prior to using them, i.e., for a 3rd party assembly. I may not have used the 3rd party assembly, but will in short order; during a quick refactor to a different namespace, ReSharper will remove my using statement. Additionally, my preference is to not remove the using statements until the end of the end of the development; at which time I'll remove the unused statements and any associated assembly references. – Metro Smurf Jan 24 '11 at 17:07
  • 2
    Don't Using statements get automatically added in when you need them? – davr Jan 24 '11 at 17:11
  • @davr - you have to add them yourself, but if the assembly is referenced VS (and I assume ReSharper) will work out which ones it needs to add. – ChrisF Feb 06 '11 at 21:23
  • F12 and an ALT-F5 will add missing usings for you if you add code later that needs it. So not sure what the problem really is... – Sumo Aug 16 '11 at 10:33
  • @Sumo I agree. My view point is more of an OCD thing. Why doesn't class name refactoring remove unused usings, too? It's a matter of isolated functionality and consistency. Touch what you've been given permission to touch is likely a good UX principle. – Joseph Lennox Jun 02 '15 at 19:43
  • @Oded I got the same problem as the OP. I need to use a library of extension methods. I do not necessary use them, but I want to see them in the intellisense to see if there is anything I can use. "Using" them makes the IDE recognize the extension methods. However, ReSharper keeps removing these "usings", which is very annoying in my case. – Tony Oct 04 '18 at 14:44
  • Old thread I know but I have a case where I don't want System.Linq removed because I'm cross-compiling the code to compact framework but developing on the full framework. The compact doesn't bundle in System.Linq by default. – Nick Sep 11 '21 at 07:30
  • If you use compiler directives resharper thinks that code part is unused so it removes the using statements. That's a big problem! and that's why one may want to prevent this feature. – ozanmut Oct 05 '22 at 14:37

1 Answers1

13

I don't think you can do this unequivocally.

However, there is an option to specify namespaces that should not be removed (Resharper Options -> C# -> Namespace Imports), but you need to know which ones you don't want it to remove.

Joe
  • 41,484
  • 20
  • 104
  • 125