Consume Extension method in non static class

All of the examples for extension methods that I have seen consume the extension method in a class like:

class Program
{
    static void Main()
    {
        ...Call extension method here
    }
}

These seem to work because the consuming class is static. Is there a way to consume an extension method in a non static class like below? I can't seem to find any examples like this.

I have my Extension Method class:

using System;
using System.Collections.Generic;

namespace AwesomeApp
{
    public static class LinqExtensionMethods
    {
        public static IEnumerable<T> FindItemsBeforeAndAfter<T>(this IEnumerable<T> items, Predicate<T> matchFilling)
        {
            if (items == null)
                throw new ArgumentNullException("items");
            if (matchFilling == null)
                throw new ArgumentNullException("matchFilling");
            return items;
        }
    }
}

And I have my class that consumes the extention method

namespace AwesomeApp
{
    public class Leaders : ILeaders
    {
        var leaders = GetAllLeaders();

        var orderedleaders = leaders.OrderByDescending(o => o.PointsEarned);

        var test = orderedleaders.FindItemsBeforeAndAfter(w => w.UserId == 1);
    }
}

If I call the extension method from a static class I do not the the 'Extension method must be defined in a non-generic static class' error:

public class test
{
    public void testfunc()
    {
        List<int> testlist = new List<int>() {1,2,3,4,5,6,7,8,9};

        testlist.FindItemsBeforeAndAfter<int>(e => e == 5);

    }
}

I have read through all the stackoverflow answers I can find on the non-generic static class error and they deal with writing your extension method but don't deal with consuming it.

So the question is: If using an extension method with a non-static class is not possible then is there any way to do something that works in a similar way? for example it can be called as .ExtensionMethod not Helper.ExtensionMethod(passedObject)??

Resolution: I thought I cut and pasted the extension method from public class Leaders : ILeaders to its own class so that I could make it static but I actually just copied it. The compiler error was pointing to the class name so I did not see the extension method still at the bottom of the file. The error message is accurate and everyone that answered is correct.

Jon Skeet
people
quotationmark

These seem to work because the consuming class is static.

No, that's incorrect. Extension methods definitely don't have to be consumed from static classes or static methods.

However, they do have to be declared in a class which is:

  • Non-nested
  • Non-generic
  • Static

You appear to be confusing calling with declaring - when you say:

If I call the extension method from a static class I do not the the 'Extension method must be defined in a non-generic static class' error

... you'll only get that if you try to declare the method in a class which doesn't satisfy all the above criteria. You should double click on the error to show where it's being generated - I'm sure you'll find it's the declaration, not the use of the method.

Note that your final example (class test) is not a static class, nor a static method.

people

See more on this question at Stackoverflow