ZetCode

C# extension method

last modified August 30, 2021

C# extension method tutorial shows how to define and use extension methods in C# language.

C# extension method

Extension methods are methods that can be inserted to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Extension methods are static methods, but in C# they are called as if they were normal C# methods. They use this modifier in their parameters.

Extension methods are only in scope when we explicitly import the namespace into your source code with a using directive.

In the standard library, we can find plenty of extension methods, most notably in LINQ.

C# LINQ extension methods

LINQ is a mini language built into C# that brings more advanced query abilities.

Program.cs
using System;
using System.Linq;

string[] words = { "falcon", "oak", "sky", "cloud", "tree", "tea", "water" };

Console.WriteLine(words.First(word => word.Length == 3));
Console.WriteLine(words.Last(word => word.Length == 3));

The example prints the first and the last word that has three letters.

Console.WriteLine(words.First(word => word.Length == 3));
Console.WriteLine(words.Last(word => word.Length == 3));

Both First and Last are C# extension methods that are defined in the System.Linq namespace.

$ dotnet run 
oak
tea

C# extension method example

In the following example, we add a new method to the string type.

Program.cs
using System;

var msg = "We walked for hours. We saw four hawks in the sky.";

int n = msg.WordCount();
Console.WriteLine(n);

public static class ExtensionMethods
{
    public static int WordCount(this string str)
    {
        return str.Split(new char[] { ' ', '.', '?' },
            StringSplitOptions.RemoveEmptyEntries).Length;
    }
}

The WordCount method is a static methods defined in a static class. Its parameter contains the this keyword.

int n = msg.WordCount();

The method is called directly on the string.

$ dotnet run 
11 

C# extension method example II

In the next example, we add an extension method to the int type.

Program.cs
using System;

int x = 20;
int y = -5;

Console.WriteLine(x.Abs());
Console.WriteLine(y.Abs());

public static class ExtensionMethods
{
    public static int Abs(this int val)
    {
        if (val < 0)
        {
            return -val;
        }

        return val;
    }
}

The example adds the Abs method, which returns an absolute value of the given integer.

$ dotnet run 
20
5 

C# generic extension method

In the next example, we define a FindAll extension method for the list type.

ExtensionMethods
using System;
using System.Collections.Generic;

public static class ExtensionMethods
{
    public static List<T> FindAll<T>(this List<T> vals, List<Predicate<T>> preds)
    {
        List<T> data = new List<T>();

        foreach (T e in vals)
        {
            bool pass = true;

            foreach (Predicate<T> p in preds)
            {
                if (!(p(e)))
                {
                    pass = false;
                    break;
                }
            }

            if (pass) data.Add(e);
        }

        return data;
    }
}

The FindAll method returns list elements that fill all the specified predicates.

public static List<T> FindAll<T>(this List<T> vals, List<Predicate<T>> preds)

The FindAll method takes a list of generic predicate functions as a parameter. It returns a filtered generic list.

Program.cs
using System;
using System.Collections.Generic;

var preds = new List<Predicate<int>>();
preds.Add(e => e > 0);
preds.Add(e => e % 2 == 0);

var vals = new List<int> {-3, -2, -1, 0, 1, 2, 3, 4};
var filtered = vals.FindAll(preds);

foreach (var e in filtered) {

    Console.WriteLine(e);
}

Console.WriteLine("---------------------");

var words = new List<string> {"sky", "wrath", "wet", "sun", "pick", "who", 
    "cloud", "war", "water", "jump", "ocean"};

var preds2 = new List<Predicate<string>>();
preds2.Add(e => e.StartsWith("w"));
preds2.Add(e => e.Length == 3);

var filtered2 = words.FindAll(preds2);

foreach (var e in filtered2) {

    Console.WriteLine(e);
}

We define two lists: an integer list and a string list. From the integer list, we filter out all positive even values. From the string list, we get all words that start with 'w' and have three letters.

$ dotnet run 
2
4
---------------------
wet
who
war

In this tutorial, we have worked with extension methods in C#.

Visit C# tutorial or list all C# tutorials.