Skip to main content

Extension method must be defined in a non-generic static class

In This Article

CodeRush Classic shows the Extension method must be defined in a non-generic static class code issue if an extension method is defined in a generic or non-static class.

#Fix

Make the class static and non-generic or declare the extension method in another class.

#Purpose

Highlights the extension method declaration statements, which would cause the Extension method must be defined in a non-generic static class compilation error.

#Example

public class MyClass
{
    public static int Total(this int[] intArray)
    {
        int result = 0;
        foreach (int item in intArray)
            result += item;
        return result;
    }
}

Fix:

public static class MyClass
{
    public static int Total(this int[] intArray)
    {
        int result = 0;
        foreach (int item in intArray)
            result += item;
        return result;
    }
}