Extension Members in C#

 

If you remember the summary I wrote about C# 13 back in November, I mentioned a feature that wasn’t ready yet but was planned, extensions everywhere. And here we have an update about that news. With the arrival of C# 14, we will get what we call extension members.

 

For now, and until November 2025 alongside C# 14 (.NET 10), this new functionality is in preview and should not be used in production.

 

 

1 - Extension Methods

 

In C#, for many years we’ve had what are called extension methods. These are methods we can use to extend the capabilities of a class.

For example, and to use the same example as Microsoft itself used in their announcement:

 

If you have a collection of integers and want to get those greater than X, you can write code like the following:

List<int> values = new List<int> { 0,5,6,9,20,55,99 };
IEnumerable<int> result = values.WhereGreaterThan(18);

public static class Extensions{
	public static IEnumerable<int> WhereGreaterThan(this IEnumerable<int> source, int threshold)		=> source.Where(x => x > threshold);
}

Here we have a method that extends the functionality of IEnumerable using the this keyword.

In my personal opinion, this language feature is something I love and use all the time.

 

 

2 - Extension Methods Everywhere | Extension Members

 

With the arrival of C# 14 this behavior will change a bit. Currently, you need to do what we did above: create a static class with a static method, and the compiler knows how to identify the method you’re calling. Under the hood, it actually translates this:

IEnumerable<int> result = values.WhereGreaterThan(18);

to a call to the static class itself:

IEnumerable<int> result = Extensions::WhereGreaterThan(values, 18);

(the compiler does this)

 

In C# 14 or .NET 10, we will get a new keyword: extension. With this new keyword, we can change the previous code to the following:

public static class Extensions{
    extension(IEnumerable<int> source)
     {
        public IEnumerable<int> WhereGreaterThan(int threshold) => source.Where(x => x > threshold);    
     }
}

And the code works exactly the same as before.

 

 

At this point, you might be wondering what the reason is for adding a new keyword. The reason is that we are no longer limited to just adding methods—we can now also extend functionality with properties. In this case, continuing with the documentation’s example, we add a property IsEmpty:

public static class Extensions{
	extension(IEnumerable<int> source)
	{
		public IEnumerable<int> WhereGreaterThan(int threshold)
			=> source.Where(x => x > threshold);
		public bool IsEmpty => !source.Any(); 👈
	}
}

Which can be used in code as follows:

List<int> values = new List<int> { 0,5,6,9,20,55,99 };
IEnumerable<int> result = values.WhereGreaterThan(18);
if (result.IsEmpty) 👈
{
	Console.WriteLine("There is no elements on the list");
}

 

 

2.1 - Using Generics in Extension Members

 

Just like with extension methods, here you can also use generics, which means you can do something like this:

extension<T>(IEnumerable<T> source) where T : INumber<T>
{
    public IEnumerable<T> WhereGreaterThan(T threshold) => source.Where(x => x > threshold);
    public bool IsEmpty => !source.Any();
}

And in this specific case, these extensions work with any C# type that is a number: int, long, float, etc.

 

 

3 - Should We Migrate Extension Methods in C# 14?

 

One of the questions many of you might have is whether we should migrate all our extension methods to the new approach. The answer is no.

Both approaches can coexist, and there’s no need to change.

What I would personally do is keep things consistent. If you’re in a project with a lot of extension methods, just keep adding them that way. If you’re starting a new project from scratch and want to start adding them, you could discuss whether to use the new or old approach, but I don’t recommend mixing both, mainly to keep the code as clean as possible.

 

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café