Introduction
C# 14 introduces a new feature called extension members. This feature lets developers add new things—like properties, methods, fields, and events—to existing types without changing their original code. It builds on extension methods and takes them further, making code easier to write, read, and maintain.
What Are Extension Members?
Extension members let you add new instance-level features to any type so they work just like built-in members. Unlike traditional extension methods, which only allow adding static methods, extension members can also include properties and other member types that feel native to the original class.
The source code can be downloaded from GitHub
The syntax for creating extension members is simple and easy to understand:
public static class MyExtensions
{
public extension(SomeType target)
{
public int NewProperty => target.SomeCalculation();
public void NewMethod()
{
// Implementation here
}
}
}
In this example, SomeType is extended with NewProperty and NewMethod, and you can use them directly on a SomeType object just like its built-in members.
How to Use Extension Members
Using extension members is just like using built-in members. They show up in IntelliSense and work as if they were originally defined in the type.
Example Usage
SomeType obj = new SomeType();
int value = obj.NewProperty;
obj.NewMethod();
Benefits
No code changes needed: You can add features to third-party or built-in .NET types without touching their source code.
Type-safe and IDE-friendly: The compiler checks your code, and IntelliSense helps you while you type.
Cleaner code: You can keep related extensions together in static classes, making the code easier to manage.
Good performance: Extension members are compiled efficiently and add very little runtime cost.
Limitations
You cannot replace or hide existing members of the original type.
You cannot access the type’s private or internal members.
Extensions are decided at compile time, so they may not work well in dynamic scenarios.
Practical Examples
Adding a Word Count property to strings
public static class StringExtensions
{
extension(string s)
{
public int WordCount => s.Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
Usage
string text = "Hello, welcome to the world of C# 14 extension members.";
int wordCount = text.WordCount;
Console.WriteLine($"{text}");
Console.WriteLine("Word Count: " + wordCount);
Extending Custom Types
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateOnly DateofBirth { get; set; }
}
public static class EmployeeExtensions
{
extension(Employee emp)
{
public string FullName => $"{emp.FirstName} {emp.LastName}";
public int Age => DateTime.Now.Year - emp.DateofBirth.Year;
public bool IsAdult => emp.Age >= 18;
}
extension(Employee)
{
public static Employee Create(string firstName, string lastName, DateOnly dateOfBirth)
{
return new Employee
{
FirstName = firstName,
LastName = lastName,
DateofBirth = dateOfBirth
};
}
}
}
Usage
var emp = Employee.Create("John", "Doe", new DateOnly(1990, 5, 15));
Console.WriteLine($"Employee Full Name: {emp.FullName}");
Console.WriteLine($"Employee Age: {emp.Age}");
Console.WriteLine($"Is Employee Adult: {emp.IsAdult}");
![ExtensionMembersInCSharp14_01]()
For in-depth technical details , refer to the original article.
1. Microsoft learning on Extension Members
2. Microsoft learning on Extension Declarations
Conclusion
Extension members in C# 14 are a big improvement that helps developers write cleaner and more modular code. They let you add useful features to existing types without using inheritance or wrapper classes, making your code simpler and better organized.
Happy Coding!