-1

enter image description here public class countryController : Controller //Error :Extension method must be defined in a non-generic static class

Rics
  • 1
  • 1
  • 4

2 Answers2

3

You can't define an extension method inside a non-static class countryController

This is not allowed:

public class MyExtensions
{
    public static void SomeExtension(this String str)
    {

    }
}

This is allowed:

public static class MyExtensions
{
    public static void SomeExtension(this String str)
    {

    }
}

You have a method whose first parameter starts with this, you need to find it and either modify it by removing the this or move it to a static helper class.

According to C# Specifications:

10.6.9 Extension methods

When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a pointer type.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
0

Add keyword static to class declaration:

// this is a non-generic static class

public static class yourclass
{
}

Following points need to be considered when creating an extension method:

The class which defines an extension method must be non-generic, static and non-nested

Every extension method must be a static method

The first parameter of the extension method should use the this keyword.

MMM
  • 3,132
  • 3
  • 20
  • 32