Read the syntax
An extension method is a static method in a static class whose first parameter begins with this. It can be called with instance-style syntax, but it does not modify the target type’s declaration.
LINQ itself uses extension methods on enumerable sequences. Scope and overload resolution still matter; extensions cannot access a target object’s private members.
Syntax
public static returnType MethodName(this Type target, ...) { }Examples
Basic Extension Method
Adding a new method to the built-in string type.
string word = "racecar";
Console.WriteLine(word.IsPalindrome()); // true - called just like a regular method
public static class StringExtensions
{
public static bool IsPalindrome(this string text)
{
string cleaned = text.ToLower().Replace(" ", "");
return cleaned == new string(cleaned.Reverse().ToArray());
}
}- Adding a new method to the built-in string type.
- An extension method is a static method in a static class whose first parameter begins with this. It can be called with instance-style syntax, but it does not modify the target type’s declaration.
- IsPalindrome lowercases text, removes spaces and compares it with a reversed copy. racecar matches its reverse, so the call prints True.
Extension Methods on Collections
A very common use case, similar to how LINQ itself extends IEnumerable<T>.
List<int> data = new List<int> { 5, 1, 4, 2, 3 };
Console.WriteLine(data.Median()); // 3
public static class ListExtensions
{
public static double Median(this List<int> numbers)
{
var sorted = numbers.OrderBy(n => n).ToList();
int mid = sorted.Count / 2;
return sorted.Count % 2 == 0
? (sorted[mid - 1] + sorted[mid]) / 2.0
: sorted[mid];
}
}- A very common use case, similar to how LINQ itself extends IEnumerable<T>.
- An extension method is a static method in a static class whose first parameter begins with this. It can be called with instance-style syntax, but it does not modify the target type’s declaration.
- Median sorts a copy of the input, finds its middle index and returns that value for five items. The original list is unchanged and the output is 3.
Chaining Extension Methods
Extension methods work seamlessly alongside built-in and LINQ methods in a fluent chain.
List<int> numbers = Enumerable.Range(1, 20).ToList();
var primes = numbers.Where(n => n.IsPrime()).ToList();
Console.WriteLine(string.Join(", ", primes)); // 2, 3, 5, 7, 11, 13, 17, 19
public static class IntExtensions
{
public static bool IsPrime(this int n)
{
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
}- Extension methods work seamlessly alongside built-in and LINQ methods in a fluent chain.
- An extension method is a static method in a static class whose first parameter begins with this. It can be called with instance-style syntax, but it does not modify the target type’s declaration.
- Range creates 1 through 20; Where keeps numbers for which IsPrime returns true. Join prints the eight primes in that range.
Best practices
- Use extension methods to add genuinely reusable helper functionality to a type, especially one you don't own and can't otherwise modify
- Put extension methods in a well-named static class (StringExtensions, DateTimeExtensions) so they're easy to find and organize
- Don't overuse extension methods for logic that would fit more naturally as a regular method on a type you do control - reserve them for genuinely external types or cross-cutting helpers
- Remember an extension method never overrides a real instance method with the same signature - a genuine instance method always takes priority if one exists
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference