C# extension members
last modified November 15, 2026
This article shows how to use extension members in C#. We cover extension
blocks, extension properties, static extension members, generic extension
blocks, extension operators, ref receivers, and compatibility
with classic extension methods.
Extension methods have been part of C# since version 3.0. They are static
methods whose first parameter carries the this modifier; they
can be called as if they were instance methods of the extended type.
C# 14 introduced a more general syntax, the extension block:
static class StringExtensions
{
extension(string s)
{
public int WordCount => s.Split(' ').Length;
}
}
The extension keyword is followed by the receiver,
the type being extended, optionally with a parameter name. Inside the block
we declare members as if they were members of that type. Besides methods,
the block may contain properties, static methods, static properties, and
operators. Extension blocks must be placed in a non-nested, non-generic
static class, the same as classic extension methods.
Extension methods in a block
The first example declares two extension methods with the new syntax.
string msg = "an old falcon flies high";
Console.WriteLine(msg.Capitalize());
Console.WriteLine(msg.Repeat(2));
static class StringExtensions
{
extension(string s)
{
public string Capitalize() =>
s.Length == 0 ? s : char.ToUpper(s[0]) + s[1..];
public string Repeat(int n) => string.Concat(Enumerable.Repeat(s, n));
}
}
The methods extend the string type.
extension(string s)
{
public string Capitalize() =>
s.Length == 0 ? s : char.ToUpper(s[0]) + s[1..];
The receiver parameter s is shared by all members of the
block. The methods do not repeat the this string s parameter;
they look like ordinary instance methods.
Console.WriteLine(msg.Capitalize()); Console.WriteLine(msg.Repeat(2));
The call sites are the same as for classic extension methods.
$ dotnet run An old falcon flies high an old falcon flies highan old falcon flies high
Extension properties
Extension properties were the most requested feature that classic extension methods could not provide. With extension blocks, we can add computed properties to existing types.
string[] texts = ["an old falcon", " ", "", "sky"];
foreach (var text in texts)
{
Console.WriteLine($"'{text}': blank: {text.IsBlank}, words: {text.WordCount}");
}
static class StringExtensions
{
extension(string s)
{
public bool IsBlank => string.IsNullOrWhiteSpace(s);
public int WordCount =>
s.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length;
}
}
We add the IsBlank and WordCount properties to
strings.
public bool IsBlank => string.IsNullOrWhiteSpace(s);
An extension property has a getter and optionally a setter. It cannot have a backing field, so auto-implemented extension properties are not allowed; the property must compute its value from the receiver.
Console.WriteLine($"'{text}': blank: {text.IsBlank}, words: {text.WordCount}");
The properties are used without parentheses, exactly like the built-in
Length property.
$ dotnet run 'an old falcon': blank: False, words: 3 ' ': blank: True, words: 0 '': blank: True, words: 0 'sky': blank: False, words: 1
Static extension members
If the receiver has no parameter name, the block declares static members. They are called on the type itself, not on an instance.
TimeSpan sprint = TimeSpan.FromWeeks(2);
Console.WriteLine(sprint);
Console.WriteLine(DateOnly.Easter(2026).ToString("yyyy-MM-dd"));
Console.WriteLine(DateOnly.Easter(2027).ToString("yyyy-MM-dd"));
static class DateExtensions
{
extension(TimeSpan)
{
public static TimeSpan FromWeeks(int weeks) => TimeSpan.FromDays(weeks * 7);
}
extension(DateOnly)
{
public static DateOnly Easter(int year)
{
int a = year % 19, b = year / 100, c = year % 100;
int d = b / 4, e = b % 4, f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30;
int i = c / 4, k = c % 4;
int l = (32 + 2 * e + 2 * i - h - k) % 7;
int m = (a + 11 * h + 22 * l) / 451;
int month = (h + l - 7 * m + 114) / 31;
int day = (h + l - 7 * m + 114) % 31 + 1;
return new DateOnly(year, month, day);
}
}
}
We add a factory method to TimeSpan and a method computing
the date of Easter to DateOnly.
extension(TimeSpan)
{
public static TimeSpan FromWeeks(int weeks) => TimeSpan.FromDays(weeks * 7);
}
The receiver is only a type; there is no instance to work with. The
TimeSpan type has FromDays and
FromHours, but no FromWeeks; now it has.
TimeSpan sprint = TimeSpan.FromWeeks(2);
The static extension method is called with the type name. The
Easter method implements the anonymous Gregorian algorithm
(also known as the Meeus/Jones/Butcher algorithm).
$ dotnet run 14.00:00:00 2026-04-05 2027-03-28
Generic extension blocks
The extension block may declare type parameters. All members of the block share them.
List<int> vals = [1, 2, 3, 4, 5, 6, 7];
string[] empty = [];
Console.WriteLine(vals.IsEmpty);
Console.WriteLine(empty.IsEmpty);
Console.WriteLine(string.Join(" ", vals.EveryOther()));
Console.WriteLine(string.Join(" ", "falcon".EveryOther()));
static class EnumerableExtensions
{
extension<T>(IEnumerable<T> source)
{
public bool IsEmpty => !source.Any();
public IEnumerable<T> EveryOther() => source.Where((_, i) => i % 2 == 0);
}
}
We extend every IEnumerable<T> with a property and a
method.
extension<T>(IEnumerable<T> source)
The type parameter T is declared on the block. Lists, arrays,
and strings all implement IEnumerable<T>, so the members
are available for all of them.
public IEnumerable<T> EveryOther() => source.Where((_, i) => i % 2 == 0);
The method returns the elements at even indexes. The Where
overload with an index parameter makes this a one-liner.
$ dotnet run False True 1 3 5 7 f l o
Constraints
The type parameters of an extension block can be constrained. Combined with generic math interfaces, a single block extends all numeric types.
int[] vals = [-3, 0, 7];
double[] dvals = [2.5, -0.5];
foreach (var v in vals)
{
Console.WriteLine($"{v}: above zero {v.IsAboveZero}, squared {v.Squared()}");
}
foreach (var v in dvals)
{
Console.WriteLine($"{v}: above zero {v.IsAboveZero}, squared {v.Squared()}");
}
static class NumberExtensions
{
extension<T>(T n) where T : System.Numerics.INumber<T>
{
public bool IsAboveZero => n > T.Zero;
public T Squared() => n * n;
}
}
The members are available for any type that implements
INumber<T>.
extension<T>(T n) where T : System.Numerics.INumber<T>
{
public bool IsAboveZero => n > T.Zero;
The constraint allows us to use the > operator and the
T.Zero static property inside the block. Extension members do
not hide existing members: if the type already has a member of the same
name, the type's member wins. For instance, a property named
IsPositive would conflict with the static
int.IsPositive method and the call v.IsPositive
would not compile.
$ dotnet run -3: above zero False, squared 9 0: above zero False, squared 0 7: above zero True, squared 49 2.5: above zero True, squared 6.25 -0.5: above zero False, squared 0.25
Extension operators
C# 14 also allows us to define operators for types we do not own. Operators are static, so they go into a block without a receiver name.
var v1 = new Vec(1, 2);
var v2 = new Vec(3, 4);
Console.WriteLine(v1 + v2);
Console.WriteLine(v1 * 3);
record struct Vec(int X, int Y);
static class VecExtensions
{
extension(Vec)
{
public static Vec operator +(Vec a, Vec b) => new(a.X + b.X, a.Y + b.Y);
public static Vec operator *(Vec a, int k) => new(a.X * k, a.Y * k);
}
}
We define addition and scalar multiplication for the Vec
struct outside of the struct.
public static Vec operator +(Vec a, Vec b) => new(a.X + b.X, a.Y + b.Y);
The rules are the same as for operators declared inside a type: at least one of the parameters must be of the extended type.
$ dotnet run
Vec { X = 4, Y = 6 }
Vec { X = 3, Y = 6 }
ref receivers
For value types, the receiver can be passed by reference. The extension members then modify the original variable instead of a copy.
int n = 5;
n.Increment();
n.Increment();
Console.WriteLine(n);
var p = new Point(1, 1);
p.Move(2, 3);
Console.WriteLine(p);
struct Point(int x, int y)
{
public int X = x;
public int Y = y;
public override string ToString() => $"({X}, {Y})";
}
static class RefExtensions
{
extension(ref int n)
{
public void Increment() => n++;
}
extension(ref Point p)
{
public void Move(int dx, int dy)
{
p.X += dx;
p.Y += dy;
}
}
}
We define an Increment method for integers and a
Move method for a mutable struct.
extension(ref int n)
{
public void Increment() => n++;
}
With the ref modifier, the method increments the variable on
which it is called. Without it, the method would increment a copy and the
change would be lost.
n.Increment(); n.Increment();
The variable n ends up as 7. A ref receiver must
be a value type, and the method can only be called on variables, not on
literals or other values.
$ dotnet run 7 (3, 4)
Compatibility with extension methods
Extension blocks and classic extension methods can be mixed in the same class. An extension method declared in a block can be called in the static form too, the same way as a classic one.
string word = "falcon";
Console.WriteLine(word.Shout());
Console.WriteLine(TextExtensions.Shout(word));
Console.WriteLine(word.Whisper());
Console.WriteLine(TextExtensions.Whisper(word));
Console.WriteLine(word.Size);
Console.WriteLine(TextExtensions.get_Size(word));
static class TextExtensions
{
public static string Whisper(this string s) => s.ToLower() + "...";
extension(string s)
{
public string Shout() => s.ToUpper() + "!";
public int Size => s.Length;
}
}
The class has a classic extension method and an extension block.
public static string Whisper(this string s) => s.ToLower() + "...";
This is the original syntax. Existing extension methods do not need to be rewritten; the two forms are source and binary compatible.
Console.WriteLine(TextExtensions.Shout(word));
The compiler turns an extension block method into a static method with the receiver as the first parameter, so it can be called directly.
Console.WriteLine(TextExtensions.get_Size(word));
An extension property is compiled into a static accessor method. The
getter of Size becomes get_Size. Calling it
directly is possible, but it is useful mainly in languages that do not
understand extension properties.
$ dotnet run FALCON! FALCON! falcon... falcon... 6 6
Source
In this article we have worked with extension members in C#. See also the extension method, generic math, generics, and operator overloading tutorials.
Author
List all C# tutorials.