Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Extension methods in C#

Reference for C# extension methods: declare static methods with a this parameter, call them like instance methods, resolution rules, null handling and generic extensions.

An extension method is a static method that you call as if it were an instance method of another type. You declare it in a non-generic, non-nested static class, and mark its first parameter with this. The compiler rewrites text.Shout() into StringExtensions.Shout(text). Nothing is added to the extended type, and it can only reach that type's public members.

This is how LINQ works: Where, Select and the rest are extension methods on IEnumerable<T>. You can extend classes, structs, interfaces, enums and generic types, even ones you do not own.

C#
static class StringExtensions
{
    public static string Shout(this string s) => s.ToUpperInvariant() + "!";
}

Try it

C# Writing and calling extensions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Instance methods win, generic and interface extensions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static class Name                       // static, non-generic, top-level
{
    public static R Method(this T self, P1 p1);                 // instance-style
    public static R Method<T>(this IEnumerable<T> self);        // generic
    public static void Reset(this ref int x);                   // ref/in on struct receiver
}
Part Description
this T self The extended type; must be the first parameter. Allowed for any type: class, struct, interface, enum, delegate, array, generic.
Class Must be static, non-generic and not nested.
Call self.Method(p1), or Name.Method(self, p1) as an ordinary static call.
using The static class's namespace must be in scope (a using directive or the same namespace).

Errors: CS1106 if the class is not a non-generic static class; CS1105 ("Extension method must be static") if the method is not static; CS1061 when the namespace is not imported and the compiler cannot find the method.

Usage notes

  • Instance methods win. If the type has an applicable instance method with the same name, the extension is never called, silently. Adding a method to a type later can change what code does.
  • Nulls. The receiver may be null and the call does not throw by itself, because it is a static call. Guard with ArgumentNullException.ThrowIfNull(self) or design for it, as OrDefault does.
  • Only public API. An extension cannot see private or internal (from another assembly) members.
  • Namespace discipline. Put extensions in a namespace people opt into. LINQ lives in System.Linq for that reason; a global using makes it implicit.
  • Interfaces as receivers. Extending IEnumerable<T> or IGreeter makes the method available on every implementation, with no inheritance.
  • Value-type receivers. A this parameter of struct type receives a copy; use this ref or this in to mutate or avoid copying.
  • Not for virtual dispatch. Extensions are resolved at compile time from the static type. For polymorphic behavior use an interface member or default interface members.
  • C# 14 adds extension members, which also allow extension properties and static members.