ReSharper 2018.3 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 MetaInfo class into an instance method of the same type:

Before refactoring

After refactoring

class MetaInfo { public string Id { get; set; } public string Name { get; set; } public static MetaInfo Merge(MetaInfo m1, MetaInfo m2) { return new MetaInfo { Id = m1.Id + m2.Id, Name = m1.Name + m2.Name }; } } class Test { public Test() { var info1 = new MetaInfo(){Id = "123", Name = "AA"}; var info2 = new MetaInfo(){Id = "456", Name = "BB"}; var merged = MetaInfo.Merge(info1, info2); } }

class MetaInfo { public string Id { get; set; } public string Name { get; set; } public MetaInfo Merge(MetaInfo m2) { return new MetaInfo { Id = Id + m2.Id, Name = Name + m2.Name }; } } class Test { public Test() { var info1 = new MetaInfo(){Id = "123", Name = "AA"}; var info2 = new MetaInfo(){Id = "456", Name = "BB"}; var merged = info1.Merge(info2); } }

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

Before refactoring

After refactoring

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

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

To 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 Ctrl+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.

Refactorings Make Method Non Static dialog box

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available

Feature is available

Feature is not available

Feature is not available

Feature is not available

Feature is available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

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: 25 April 2019

See Also