Convert Static to Extension Method refactoring
Last modified: 01 July 2022This refactoring helps you convert a static method to an extension method, provided that the static method:
Has at least one parameter.
Resides in a non-generic, non-nested static class.
Consider the following example:
Before refactoring
static class Foo{ public static string Reverse(string input) { var chars = input.ToCharArray(); Array.Reverse(chars); return new string(chars); } static void Test(string str) { str = Reverse(str); }}
After refactoring
static class Foo{ public static string Reverse(this string input) { var chars = input.ToCharArray(); Array.Reverse(chars); return new string(chars); } static void Test(string str) { str = str.Reverse(); }}
tip
The reverse functionality is available with the Convert Extension Method to Plain Static refactoring refactoring.
Convert a static method into an extension method
Place the caret at the declaration or a usage of a static method in the editor, or select it in the Structure window.
Do one of the following:
Press Alt+Enter and choose Convert Static Method to Extension.
Press Ctrl+Shift+R and then choose Convert Static Method to Extension.
Choose Refactor | Convert Static Method to Extension from the main menu.
If no conflicts are found, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
tip
Right after the refactoring has been completed, all changes that it makes anywhere, including other files, are registered as a single operation. So you can use the Undo action Ctrl+Z to roll back all these changes with a single keystroke.