JetBrains Rider 2024.2 Help

Make Method/Property Static refactoring

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, JetBrains Rider 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 Info class into a static method. After refactoring, usages of instance properties Id and Name are applied to the new parameter i1:

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

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 Structure window window.

  2. Do one of the following:

    • Press Ctrl+Alt+Shift+T and then choose Make Static.

    • Choose Refactor | Make Static from the main menu.

    The Make Static dialog will open.

  3. If the method uses instance members, it is recommended to select the option Add 'this' as parameter of [type name]. In this case, JetBrains Rider 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 might 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, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

JetBrains Rider. 'Make Static' refactoring
Last modified: 23 September 2024