Extension methods have been part of C# for a long time. They allow developers to add methods to existing types without modifying their original implementation or creating a derived class. This feature is widely used in .NET applications, especially in LINQ, dependency injection, ASP.NET Core, and reusable library development.
For example, developers can call Where(), Select(), and OrderBy() directly on collections because LINQ provides these operations through extension methods.
Traditional extension methods work well, but they have some limitations. They require static classes, static methods, and a specific parameter declaration for the object being extended. As libraries grow, managing large collections of extension methods can become difficult.
C# 15 introduces extension members, providing a more flexible way to define extensions. Instead of limiting extensions to methods, the feature allows developers to define additional members, including properties and static members, for existing types.
This change gives developers more options when designing reusable APIs. It also introduces new considerations around naming, discoverability, compatibility, and maintainability.
This article explores how extension APIs work, how extension members change the design, and how to use the feature to build cleaner C# libraries.
Understanding Traditional Extension Methods
Before exploring extension members, it is useful to understand how traditional extension methods work.
An extension method is a static method declared inside a static class. Its first parameter uses the this keyword to identify the type being extended.
For example, consider a string extension that checks whether a value contains meaningful text:
public static class StringExtensions
{
public static bool HasValue(this string? value)
{
return !string.IsNullOrWhiteSpace(value);
}
}The method can be called as if it were an instance method of the string type:
string name = "Amit";
bool result = name.HasValue();The original string type does not need to be modified. The compiler resolves the extension method through the namespaces available in the current file.
This approach is simple and works well for many scenarios.
However, extension methods are still static methods. They cannot directly add instance state to the extended type, and they cannot add an actual property to that type.
For example, if a library needs to provide a calculated property for strings, traditional extension methods require a method call:
string value = "ASP.NET Core";
int length = value.GetTextLength();Developers cannot write the following using a traditional extension method:
int length = value.TextLength;This is one of the areas where extension members provide a more natural API design.
What Are Extension Members?
Extension members allow developers to define additional members for an existing type without changing the original type.
Instead of declaring every extension as a separate static method, developers can group related extensions inside an extension block.
For example, a string extension can be written using the following structure:
public static class StringExtensions
{
extension(string value)
{
public bool HasValue =>
!string.IsNullOrWhiteSpace(value);
public int TextLength =>
value?.Length ?? 0;
}
}The extension block identifies the type being extended. The members inside that block can use the extended value directly.
The extension can then be used through property syntax:
string name = "ASP.NET Core";
Console.WriteLine(name.HasValue);
Console.WriteLine(name.TextLength);This makes the API look more like a natural part of the original type.
The important distinction is that the extension does not modify the actual string class. It provides additional functionality that the compiler makes available when the relevant extension declaration is in scope.
Extension members can also include methods and other supported member forms. This makes them useful when a library needs to expose a group of related operations rather than a collection of unrelated static methods.
Creating Cleaner APIs With Extension Properties
Extension properties can improve readability when a value represents a simple calculation or a commonly used condition.
Consider a string validation scenario.
With traditional extension methods, a developer might write:
public static class StringExtensions
{
public static bool IsEmpty(this string? value)
{
return string.IsNullOrEmpty(value);
}
public static bool IsNotEmpty(this string? value)
{
return !string.IsNullOrEmpty(value);
}
}The methods are easy to understand, but their usage looks like a series of operations:
string? email = "[email protected]";
if (email.IsNotEmpty())
{
Console.WriteLine("Email is available.");
}With extension members, related checks can be expressed as properties:
public static class StringExtensions
{
extension(string? value)
{
public bool IsEmpty =>
string.IsNullOrEmpty(value);
public bool IsNotEmpty =>
!string.IsNullOrEmpty(value);
}
}The calling code becomes:
string? email = "[email protected]";
if (email.IsNotEmpty)
{
Console.WriteLine("Email is available.");
}This syntax can be easier to read when the operation is a simple calculation that does not require arguments or produce side effects.
However, developers should not convert every extension method into a property.
A method is generally more appropriate when an operation accepts parameters, performs an action, or communicates that work is being performed.
For example, sending an email, saving a record, or validating a complex document should remain a method rather than being represented as a property.
The goal is not to use properties everywhere. It is to choose an API shape that communicates the behavior clearly.
Organizing Related Extension Members
Traditional extension methods are often placed in static classes based on the type they extend.
For example, a project might contain separate classes for strings, dates, collections, and numeric values.
Extension blocks provide another way to organize these APIs.
public static class DateTimeExtensions
{
extension(DateTime value)
{
public bool IsWeekend =>
value.DayOfWeek is
DayOfWeek.Saturday or
DayOfWeek.Sunday;
public DateOnly Date =>
DateOnly.FromDateTime(value);
public bool IsPast =>
value < DateTime.Now;
}
}The calling code can use the members directly:
var orderDate = DateTime.Now;
if (orderDate.IsWeekend)
{
Console.WriteLine("The order was created on a weekend.");
}
DateOnly date = orderDate.Date;The extension block groups related behavior in one place.
For a shared library, this can make the API easier to explore. Developers can find related functionality without searching through many individual extension methods.
However, grouping should follow a clear design principle. A class should not become a collection of unrelated extensions simply because they are all static.
For example, date formatting, time-zone conversion, business-day calculations, and financial calendar rules may deserve separate extension groups because they represent different responsibilities.
A well-organized extension API should be easy to discover and should communicate why each member belongs to the extended type.
Using Extension Members With Generic Types
Extension APIs are also useful when working with generic types.
Consider a common requirement in application development: checking whether a collection contains any elements.
A traditional extension method might look like this:
public static class CollectionExtensions
{
public static bool HasItems<T>(
this IEnumerable<T> source)
{
return source is not null &&
source.Any();
}
}The calling code is straightforward:
IEnumerable<int> numbers = [1, 2, 3];
if (numbers.HasItems())
{
Console.WriteLine("The collection contains data.");
}An extension member can express the same behavior as a property:
Join the conversation! Your thoughts help the community grow.