Skip to main content

Remove Type Qualifier

  • 3 minutes to read

Purpose

Removes a redundant type qualifier from a selected type reference and adds the corresponding using/import reference to the current file if this reference does not exist.

screencast

Availability

Available when the cursor is in a fully-qualified reference to a type.

Usage

  1. Place the caret in a fully-qualified type reference.

    namespace CRRDemo {
        class Program {
            static void Main(string[] args) {
                var o = new System.Text.StringBuilder();
            }
        }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select the Remove Type Qualifier menu item.

    remove-type-qualifier

  4. Press Enter to apply this refactoring.

After execution, this refactoring removes the namespace from a selected type reference and ensures the namespace is referenced.

using System.Text;

namespace CRRDemo {
    class Program {
        static void Main(string[] args) {
            var o = new StringBuilder();
        }
    }
}

Blazor Support

You can apply the Remove Type Qualifier refactoring from the @code section in a .razor file (see the screencast below).

blazor-support

Remove All Type Qualifiers

CodeRush allows you to remove all type qualifiers that match a selected type qualifier in a file.

Place the caret in a fully-qualified type reference, invoke the Code Actions menu, select Remove All Type Qualifiers, and press Enter.

blazor-support

To remove all redundant type qualifiers in a file, apply the Remove redundant type qualifiers code cleanup rule.

Open the Editor | C# (Visual Basic) | Code Cleanup options page and enable the Remove redundant type qualifiers code cleanup rule.

configure-cleanup

You can also enable the corresponding options to apply this rule in code cleanup before a file is saved.

For more information on how to activate the code cleanup rule, see the following topic: Configure Cleanup.

Run code cleanup.

apply-cleanup

Customization

Configure Programming Style Rule

CodeRush can place import/using references inside the active namespace when you apply code cleanup or run the Remove Type Qualifier or Remove All Type Qualifiers refactoring.

To configure this behavior: open the Editor | C# (Visual Basic) | Programming Style options page, set the Namespace references declaration style option to “Place namespace import/using references inside the active namespace”, and click OK to apply this change.

configure-style-rule

For more information on the namespace reference declaration style rule, see the following topic: Programming Style Rules.

The following screencast shows how CodeRush applies the programming style rule when you run the Remove All Type Qualifiers refactoring:

place-qualifiers-inside-active-namespace

See Also