简体   繁体   中英

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;

ReSharper offers a much better way to do this:

在此处输入图片说明

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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM