Skip to main content

Refactoring Assistance

  • 3 minutes to read

Refactorings are small changes to working code that improve the internal structure without altering the external behavior of that code.

The Refactorings section describes each refactoring in detail. The description includes the purpose of the refactoring, its availability, and the “how to use” example.

How to Use

  1. Invoke the Code Actions Menu. Place the caret in the part of the code you want to change, for example, on a boolean method parameter, and press Ctrl + . or Ctrl + ~.

    ActionsMenu

    CodeRush adds the following items to Visual Studio’s light bulb menu (Code Actions Menu):

    • Code Formatters — improve code readability and layout to match a certain style without code behavior change. For example, Sort Namespace References.
    • Refactorings — simplify code, improve readability, make code more flexible (for future changes), or bring the code to meet certain standard without code behavior change. For example, Inline Method.
    • Code Providers — can change code behavior or declare undeclared types and members. Use them to supplement your code with blocks which you might want to type manually. For example, Create Event Trigger.
  2. Choose the action from the invoked menu. For example, Boolean to Enum.

    You can see a preview of the changes CodeRush can apply to your code. Code_Generation_Main

  3. Press Enter to generate code in the selected position.

class TestClass {
    private int TestMethod(TestMethodParam a) {
        if (a == TestMethodParam.Success)
            return 1000;
        else
            return 1024;
    }
}
public enum TestMethodParam {
    Success,
    Failure
}

CodeRush includes over 60 refactorings you can use to improve C#, Visual Basic, XAML, and Blazor code.

Refactoring C# VB Razor XAML
Boolean to Enum CRR_IconPassed CRR_IconPassed
Combine/Split Conditional(s) CRR_IconPassed CRR_IconPassed CRR_IconPassed
Compress to/Expand Null Coalescing Operation CRR_IconPassed CRR_IconPassed CRR_IconPassed
Compress to/Expand Ternary Expression CRR_IconPassed CRR_IconPassed
Compress/Expand Assignment CRR_IconPassed CRR_IconPassed CRR_IconPassed
Conditional to Switch/Switch to Conditional CRR_IconPassed CRR_IconPassed CRR_IconPassed
Convert Nested Element to Attribute/Attribute to Nested Element CRR_IconPassed
Convert to Auto-Implemented Property CRR_IconPassed CRR_IconPassed
Convert to Constant CRR_IconPassed CRR_IconPassed
Convert to Math.Pow Call CRR_IconPassed CRR_IconPassed CRR_IconPassed
Convert to Property with Backing Field CRR_IconPassed CRR_IconPassed
Convert to String Interpolation CRR_IconPassed CRR_IconPassed CRR_IconPassed
Convert to System Type/Built-In Type CRR_IconPassed CRR_IconPassed CRR_IconPassed
Convert to Tuple CRR_IconPassed CRR_IconPassed CRR_IconPassed
Convert to Initializer/Decompose Initializer CRR_IconPassed CRR_IconPassed CRR_IconPassed
Declare Event Handler (Razor) CRR_IconPassed
Declare Partial Class Part/Partial Record Part CRR_IconPassed CRR_IconPassed
Decompose Parameter CRR_IconPassed CRR_IconPassed
Discard Variable CRR_IconPassed CRR_IconPassed
Extract Interface CRR_IconPassed CRR_IconPassed
Extract String to Resource CRR_IconPassed CRR_IconPassed
Flatten Conditional CRR_IconPassed CRR_IconPassed CRR_IconPassed
ForEach to For/For to ForEach CRR_IconPassed CRR_IconPassed CRR_IconPassed
ForEach to Linq CRR_IconPassed CRR_IconPassed CRR_IconPassed
Import Missing Namespaces CRR_IconPassed CRR_IconPassed
Inline Lambda CRR_IconPassed CRR_IconPassed
Inline Method CRR_IconPassed CRR_IconPassed
Inline Method and Delete CRR_IconPassed CRR_IconPassed
Inline Temp CRR_IconPassed CRR_IconPassed
Introduce Using Statement CRR_IconPassed CRR_IconPassed CRR_IconPassed
Make Implicit/Explicit CRR_IconPassed CRR_IconPassed CRR_IconPassed
Make Read-Only CRR_IconPassed CRR_IconPassed
Move Declaration Near Reference CRR_IconPassed CRR_IconPassed CRR_IconPassed
Move Initialization to Declaration CRR_IconPassed CRR_IconPassed CRR_IconPassed
Move to Code Behind CRR_IconPassed
Move File to Folder CRR_IconPassed CRR_IconPassed
Move Type to File CRR_IconPassed CRR_IconPassed
Move All Types to Files CRR_IconPassed CRR_IconPassed
Move Type to Namespace CRR_IconPassed CRR_IconPassed CRR_IconPassed
Name Anonymous Type CRR_IconPassed CRR_IconPassed
Optimize Namespace References CRR_IconPassed CRR_IconPassed CRR_IconPassed
Promote to Generic Parameter CRR_IconPassed CRR_IconPassed
Pull Member Up CRR_IconPassed CRR_IconPassed
Remove Discard Variable CRR_IconPassed
Remove Variable CRR_IconPassed CRR_IconPassed
Remove Variable with its Initializer CRR_IconPassed CRR_IconPassed
Remove Redundant Assignment CRR_IconPassed CRR_IconPassed CRR_IconPassed
Remove Redundant Constructor CRR_IconPassed CRR_IconPassed
Remove Redundant Delegate Creation CRR_IconPassed CRR_IconPassed
Remove Redundant Destructor CRR_IconPassed
Remove Redundant Type Qualifier CRR_IconPassed
Remove Type Qualifier CRR_IconPassed CRR_IconPassed
Remove Unused Member CRR_IconPassed CRR_IconPassed CRR_IconPassed
Remove Unused Parameter CRR_IconPassed CRR_IconPassed CRR_IconPassed
Remove Unused Type CRR_IconPassed CRR_IconPassed
Rename CRR_IconPassed CRR_IconPassed CRR_IconPassed
Rename File to Match Type CRR_IconPassed CRR_IconPassed
Rename Namespace to Match Folder Structure CRR_IconPassed CRR_IconPassed
Rename Type to Match File CRR_IconPassed CRR_IconPassed
Reorder Parameters CRR_IconPassed CRR_IconPassed
Reverse Boolean CRR_IconPassed CRR_IconPassed CRR_IconPassed
Reverse Conditional CRR_IconPassed CRR_IconPassed CRR_IconPassed
Simplify Expression CRR_IconPassed CRR_IconPassed
Split Initialization from Declaration CRR_IconPassed CRR_IconPassed CRR_IconPassed
Split String CRR_IconPassed CRR_IconPassed CRR_IconPassed
Use Environment.NewLine CRR_IconPassed CRR_IconPassed CRR_IconPassed
Use Expression Body CRR_IconPassed CRR_IconPassed
Use NameOf CRR_IconPassed CRR_IconPassed CRR_IconPassed
Use String.Compare CRR_IconPassed CRR_IconPassed CRR_IconPassed
Use String.Format CRR_IconPassed CRR_IconPassed CRR_IconPassed
Use String.IsNullOrEmpty CRR_IconPassed CRR_IconPassed CRR_IconPassed
Use String Builder CRR_IconPassed CRR_IconPassed CRR_IconPassed
Widen Scope CRR_IconPassed CRR_IconPassed
Widen Scope (Promote to Field) CRR_IconPassed CRR_IconPassed
Refactoring C# VB Razor XAML

Define Style Rules

You can define a set of programming style rules that CodeRush applies to your code when you refactor. Refer to the Programing Style Rules topic for more information.

Blazor Support

You can apply refactorings from code sections in .razor files.

ActionsMenu

Note

The refactorings for Blazor are unable to update references in closed .razor files. Make sure all files that may be impacted by a particular refactoring are open in Visual Studio before applying it.

See Also