Skip to content

Extension members C# 14.0discovery

Add properties to existing classes just like extension methods.

Extension methods C# 2.0 allowed you to decorate existing classes with additional instance methods.

C# 14 brings a new syntax that also allows for extension properties and methods as well as static properties and methods using the new extension keyword.

Code

C#
Console.WriteLine(customer.GetFullName()); // Method
Console.WriteLine(customer.FullName); // Property

static class CustomerHelper
{
    extension(Customer c)
    {
        // Extension method
        public string GetFullName() => c.FirstName + " " + c.LastName;

        // Extension property
        string FullName => c.FirstName + " " + c.LastName;
    }
}
C#
Console.WriteLine(CustomerHelper.GetFullName(customer)); // Method

static class CustomerHelper
{
    public static string GetFullName(Customer c) => c.FirstName + " " + c.LastName;
}

More information