ReSharper 2021.1 Help

Make Method Non-Static refactoring

ReSharper | Refactor | Make Method Non-Static…
ReSharper_MakeNonStatic

This refactoring allows you to convert a static method into an instance method of the original or another type. The target type for the new instance method can be selected from one of the types passed as parameters to the method. All usages, implementations and overrides of the method are automatically updated.

This refactoring can only be applied to static methods that have at least one parameter of a type defined in the current solution.

In the example below, we use this refactoring to convert a static method Merge of the Info class into an instance method of the same type:

class Info { string Id { get; set; } string Name { get; set; } static Info Merge(Info i1, Info i2) { return new Info { Id = i1.Id + i2.Id, Name = i1.Name + i2.Name }; } void Test() { var i1 = new Info() { Id = "123", Name = "AA" }; var i2 = new Info() { Id = "456", Name = "BB" }; var merged = Info.Merge(i1, i2); } }
class Info { string Id { get; set; } string Name { get; set; } Info Merge(Info i2) { return new Info { Id = Id + i2.Id, Name = Name + i2.Name }; } void Test() { var i1 = new Info() { Id = "123", Name = "AA" }; var i2 = new Info() { Id = "456", Name = "BB" }; var merged = i1.Merge(i2); } }

In the following example, the static method Merge that works with Info objects is defined in the class Service. We use the refactoring to make Merge an instance method of the class Info, where it logically belongs:

class Service { public static Info Merge(Info i1, Info i2) { return new Info { Id = i1.Id + i2.Id, Name = i1.Name + i2.Name }; } } class Info { public string Id { get; set; } public string Name { get; set; } void Test() { var info1 = new Info() { Id = "123", Name = "AA" }; var info2 = new Info() { Id = "456", Name = "BB" }; var merged = Service.Merge(info1, info2); } }
class Service { } class Info { public string Id { get; set; } public string Name { get; set; } void Test() { var info1 = new Info() { Id = "123", Name = "AA" }; var info2 = new Info() { Id = "456", Name = "BB" }; var merged = info1.Merge(info2); } public Info Merge(Info i2) { return new Info { Id = Id + i2.Id, Name = Name + i2.Name }; } }

Convert a static method into an instance method

  1. Place the caret at the declaration or a usage of a static method in the editor, or select it in the File Structure window.

  2. Do one of the following:

    • Press Control+Shift+R and then choose Make Method Non-Static

    • Right-click and choose Refactor | Make Method Non-Static in the context menu.

    • Choose ReSharper | Refactor | Make Method Non-Static… in the main menu.

    The Make Method Non-Static dialog will open.

  3. Select a parameter from the list. The method will be converted to the instance method of the parameter type.

  4. To apply the refactoring, click Next.

  5. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

ReSharper: Make method non-static refactoring

This feature is supported in the following languages and technologies:

Language: C#Language: VB.NETLanguage: C++Language: HTMLLanguage: ASP.NETLanguage: RazorLanguage: JavaScriptLanguage: TypeScriptLanguage: CSSLanguage: XMLLanguage: XAMLLanguage: ResxLanguage: Build ScriptsLanguage: ProtobufLanguage: JSON
Feature is available in C#Feature is available in Visual Basic .NET Feature is not available in C++ Feature is not available in HTML Feature is not available in ASP.NETFeature is available in Razor Feature is not available in JavaScript Feature is not available in TypeScript Feature is not available in CSS Feature is not available in XML Feature is not available in XAML Feature is not available in Resource files Feature is not available in build script files Feature is not available in Protobuf Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

Last modified: 08 March 2021