The reverse of Convert to Extension Method: an extension method becomes an ordinary static method, and every extension-style call becomes a static call.
Arguments
Argument Meaning target.symbolthe extension method, such as M:Shop.Text.Shout(System.String)
Precondition
The method is an extension method. No call uses it through null-conditional access (value?.Shout()), whose static form would need a null check. It is not used as a method group on a value (value.Shout passed as a delegate), which has no static equivalent.
this is removed from the first parameter.Each call value.Shout(...) becomes Text.Shout(value, ...), with the receiver as the first argument. The class name is as short as the calling file allows, and a call inside the class is left unqualified. Chained calls convert together: raw.Trimmed().ToUpperInvariant() becomes Formatting.Trimmed(raw).ToUpperInvariant(). Explicit type arguments are kept. Calls already written in static form are unchanged.
Preserved
The behaviour of every call, including evaluation order: the receiver is evaluated first, as the first argument.
Limitations
LINQ query syntax that binds to the method (a Select or Where extension) is not rewritten, and stops compiling.
Error codes
Code Meaning not-extensionthe method is not an extension method conditional-accessa call uses null-conditional access method-groupthe method is used as a method group on a value
Cases
Expand all · Collapse all
Diffs show each file from before to after .
/*[*/ … /*]*/ marks a selection and /*^*/ a caret; the runner removes them before the refactoring runs.
# conditional-call-rejectedRefuses while a null-conditional call uses the extension, since the static form would change when it runs
refusal
Target symbol M:Shop.Text.Shout(System.String)
Refuses conditional-access; every file is left unchanged
Text.cs input
1 namespace Shop 2 { 3 public static class Text 4 { 5 public static string Shout (this string text) => text.ToUpperInvariant (); 6 7 public static string Maybe (string text) => text?.Shout (); 8 } 9 }
View fixture on GitHub
# genericA generic extension method keeps its explicit type arguments at call sites
success
Target symbol M:Shop.Sequences.FirstOr``1(System.Collections.Generic.IEnumerable{``0},``0)
Basket.cs modified
1 1 using System .Collections .Generic ;2 2 3 3 namespace Shop 4 4 { 5 5 public class Basket 6 6 { 7 7 private readonly List <int > _quantities = new List <int >(); 8 8 9 − public int First () => _quantities.FirstOr (0 ); 9 + public int First () => Sequences .FirstOr (_quantities, 0 ); 10 10 11 − public string FirstName (List <string > names) => names.FirstOr <string >("none" ); 11 + public string FirstName (List <string > names) => Sequences .FirstOr <string >(names, "none" ); 12 12 } 13 13 }
Sequences.cs modified
1 1 using System .Collections .Generic ;2 2 3 3 namespace Shop 4 4 { 5 5 public static class Sequences 6 6 { 7 − public static T FirstOr <T >(this IEnumerable <T > items, T fallback) 7 + public static T FirstOr <T >(IEnumerable <T > items, T fallback) 8 8 { 9 9 foreach (var item in items) 10 10 return item; 11 11 return fallback; 12 12 } 13 13 } 14 14 }
View fixture on GitHub
# not-extension-rejectedRefuses a method that is not an extension method
refusal
Target symbol M:Shop.Text.Shout(System.String)
Refuses not-extension; every file is left unchanged
Text.cs input
1 namespace Shop 2 { 3 public static class Text 4 { 5 public static string Shout (string text) => text.ToUpperInvariant (); 6 } 7 }
View fixture on GitHub
# other-namespaceCalls from a namespace that imports the extension's namespace name the class simply; chained calls keep their order
success
Target symbol M:Shop.Text.Formatting.Trimmed(System.String)
Formatting.cs modified
1 1 namespace Shop .Text 2 2 { 3 3 public static class Formatting 4 4 { 5 − public static string Trimmed (this string text) => text.Trim (); 5 + public static string Trimmed (string text) => text.Trim (); 6 6 } 7 7 }
Report.cs modified
1 1 using Shop .Text ;2 2 3 3 namespace Reports 4 4 { 5 5 public class Report 6 6 { 7 − public string Title (string raw) => raw.Trimmed ().ToUpperInvariant (); 7 + public string Title (string raw) => Formatting .Trimmed (raw).ToUpperInvariant (); 8 8 9 − public int Length (string raw) => (raw + " " ).Trimmed ().Length ; 9 + public int Length (string raw) => Formatting .Trimmed (raw + " " ).Length ; 10 10 } 11 11 }
View fixture on GitHub
# simpleDrops this from an extension method, and extension calls in other files become static calls on its class
success
Target symbol M:Shop.Text.Shout(System.String)
Checkout.cs modified
1 1 namespace Shop 2 2 { 3 3 public class Checkout 4 4 { 5 − public string Welcome (string name) => name.Shout (); 5 + public string Welcome (string name) => Text .Shout (name); 6 6 7 7 public string Static (string name) => Text .Shout (name); 8 8 } 9 9 }
Text.cs modified
1 1 namespace Shop 2 2 { 3 3 public static class Text 4 4 { 5 5 // Used for headings. 6 − public static string Shout (this string text) => text.ToUpperInvariant () + "!" ; 6 + public static string Shout (string text) => text.ToUpperInvariant () + "!" ; 7 7 8 − public static string Greet (string name) => ("hello " + name).Shout (); 8 + public static string Greet (string name) => Shout ("hello " + name); 9 9 } 10 10 }
View fixture on GitHub