Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I get the following compilation error at the class name.

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

I am not using normal class. What could be the reason for this. I don't know and don't want to use extension methods.

like image 415
softwarematter Avatar asked Nov 19 '11 13:11

softwarematter


People also ask

Can extension methods extend non-static classes?

It is compulsion that the Extension method must be in a Static class only so that only one Instance is created. For example, if you place the following in ASP.Net page it will not work. Though error will not come, but you will not see the method available.

Can we define extension methods for static class?

Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on. The parameter is preceded by the this modifier.

Are extension methods always static?

An extension method must be a static method. An extension method must be inside a static class -- the class can have any name. The parameter in an extension method should always have the "this" keyword preceding the type on which the method needs to be called.

Can a static class be generic?

Static and non-static generic methods are allowed, as well as generic class constructors.


2 Answers

As requested, here is my comment as an answer:

Without your code there isn't much we can do. My best guess is that you accidentally typed "this" somewhere in a parameter list.

like image 70
Jules Avatar answered Sep 22 '22 12:09

Jules


Sample for extension method

public static class ExtensionMethods {
 public static object ToAnOtherObject(this object obj) {
  // Your operation here
 }
}
like image 38
Peter PAD Avatar answered Sep 20 '22 12:09

Peter PAD