ReSharper 2018.3 Help

Make Method/Property Static refactoring

ReSharper | Refactor | Make Method Static…
ReSharper_MakeStatic

This refactoring allows you to convert an instance method or a property to a static one. All calls, implementations and overrides are automatically corrected.

If the converted instance method uses instance members, ReSharper helps you add the necessary parameter representing 'this' to apply these usages to. If the converted instance property uses instance members, the refactoring is not available.

In the example below, we use this refactoring to convert an instance method Merge of the MetaInfo class into a static method. After refactoring, usages of instance properties Id and Name are applied to the new parameter info1:

Before refactoring

After refactoring

class MetaInfo { public string Id { get; set; } public string Name { get; set; } public MetaInfo Merge(MetaInfo other) { return new MetaInfo { Id = Id + other.Id, Name = Name + other.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); } }

class MetaInfo { public string Id { get; set; } public string Name { get; set; } public static MetaInfo Merge( MetaInfo info1, MetaInfo other) { return new MetaInfo { Id = info1.Id + other.Id, Name = info1.Name + other.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); } }

To make a method or property static

  1. Place the caret at the declaration or a usage of an instance method or property 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 Static

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

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

    The Make Static dialog will open.
  3. If the method uses instance members, it is recommended to leave the option Add 'this' as parameter of [type name] selected. In this case, ReSharper adds the necessary parameter to apply these usages to. If necessary, you can either go without adding the parameter or apply the parameter to some of the instance member usages. In these cases, after refactoring you will have to correct the invalid usages of instance in static context manually.

  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 Static' refactoring

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: 18 December 2018

See Also