Creating Extension Methods only takes a few steps with ReSharper :
1. Declare your intent
First tell ReSharper what you want to do. Note that in the line below, I have specified a local variable and an input parameter for the method to create. This will allow ReSharper to infer both the return type and the method parameter type.
2. Create static class
With your cursor on “StringExtensions”, press ALT + Enter (if you are using the Visual Studio shortcut keyboard bindings). In the Context Actions dialog, choose “Create static class” and press enter.
ReSharper will generate the new class for you.
3. Create method
Move your cursor to the method for which you want to generate the Extension Method. Open again the Context Actions dialog, select the first option and press enter.
The method is automatically added to the new class.
4. Static to Extension Method
Finally we need to convert our new static method to an Extension method. Highlight or set the cursor on the static method to convert, then open the “Refactor This” menu (Ctrl + Shift + R). Choose the last item to complete the process.
ReSharper will not only refactor the method’s signature to add the ‘this’ keyword but also replace the original line to use the new Extension Method.
All you have to do now is to focus on the new method’s implementation.
Now in action:
Note: The screencast is heavily edited as my Dev machine simply can’t cope with Visual Studio and Camtasia running at the same time.
“Progress is made by lazy men looking for easier ways to do things”
Robert A. HeinleinW (American science-fiction writer, 1907-1988)
Let ReSharper do the work!