Rebourne07
Rebourne07

Reputation: 47

How to set some using statements as not redundant even if they are?

Often files when created start with a set of using statements that are common. Sometimes even after fleshing out the class I have no need of a few of the auto-generated using statements. However, removing them can cause problems if they are eventually needed, such as the problems caused by removing using System.Linq; Is there a way to tell Visual Studio / Resharper not to complain that certain using statements are redundant?

Example:

using System;
using System.Collections.Generic; // Don't need but want anyway without an error
using System.Linq; // Don't need but want anyway without an error
using System.Net;
using System.Text; // Don't need but want anyway without an error
using Acceptance.EndToEnd.Helpers;
using Acceptance.EndToEnd.Locators;

Upvotes: 3

Views: 343

Answers (3)

ChrisF
ChrisF

Reputation: 137148

You might as well remove them, but as has been pointed out, leaving them in does no harm.

If you do remove them, Visual Studio/ReSharper will add them back in as needed - even System.Linq if you use ReSharper.

If you really want, you can stop ReSharper complaining by turning off this warning when you click on the lightbulb:

enter image description here

Upvotes: 1

Matthias
Matthias

Reputation: 16209

ReSharper offers a much better way to do this:

enter image description here

Upvotes: 6

René Vogt
René Vogt

Reputation: 43896

You could use ChrisF's answer or you could add ReSharper comments to disable the warnings in your code.

Use

// ReSharper disable RedundantUsingDirective

at the beginning of the file to disable the warnings in the whole file or use

// ReSharper disable once RedundantUsingDirective
using Namespace.That.I.Dont.Need

to disable warnings for single using statements or use

// ReSharper disable RedundantUsingDirective
using Namespace.That.I.Dont.Need
using Another.Namespace.That.I.Dont.Need
// ReSharper restore RedundantUsingDirective
using Namespace.That.I.Do.Need

for multiple namespaces.

Upvotes: 0

Related Questions