0

Im pretty new to C# and got myself a project from a friend and I'm having this error when I'm trying to compile it:

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

The code looks like this:

public partial class _Default : System.Web.UI.Page {

Probably a very simple problem but I cant figure it out.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Ginger
  • 1
  • 1
  • 1
    Is your intention to add an extension method or by mistake you added this keyword in method parameter? If latter, just drop the this keyword from parameter, if former then [do what compiler says](http://stackoverflow.com/questions/6096299/extension-methods-must-be-defined-in-a-non-generic-static-class) – Sriram Sakthivel Oct 28 '14 at 08:42

4 Answers4

0

You're probably trying to define an extension method in that class.

An extension method is marked with the static modifier and its first argument with the this modifier, e.g.:

public static void Method(this string arg) {}

If you added the this modifier by mistake, simply remove it to make it a normal static method. If you did it on purpose, move the method declaration to a static non-generic non-nested class.

dcastro
  • 66,540
  • 21
  • 145
  • 155
0

Well, it's telling you exactly what's wrong - you can't put an extension method (a static method whose first parameter is preceded by this) in anything other than a non-generic static class. Look for a method (or methods) where the first parameter is preceded by this, and move them to a new, non-generic static class:

public static class SomeExtensions
{
    // Extension methods go here, e.g.
    public static void SomeMethod(this SomeClass firstParam, int secondParam)
    {
        // ...
    }
}
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
0

You are using the this keyword in a method signature, indicating it is an extension method.

You should create a static class to contain those methods. The signature must be something like this:

public static class PageExtensionMethods
{
    public static void SomeMethod(this SomeType s)
    { }
}

Make sure though, you really intent to use extension methods. If it is just a method you want to call, it doesn't really have to be an extension method. Remove the this keyword then.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

Just do as complier wants it should be done:

  // Public static non generic class (exactly what compiler wants)
  public static class PageExtensions {
    ...
    // Your extension method for System.Web.UI.Page
    public static void MyExtensionMethod(this System.Web.UI.Page value) {
      ...
    }
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215