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.
Actually I'm answering the question of why extension methods cannot work with static classes. The answer is because extension methods are compiled into static methods that cannot recieve a reference to a static class.
No. Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don't need an extension method since you can just add the method directly.
Yes. It is compulsion that the Extension method must be in a Static class only so that only one Instance is created.
change
public class LinqHelper
to
public static class LinqHelper
Following points need to be considered when creating an extension method:
non-generic
, static
and non-nested
static
methodthis
keyword. if you do not intend to have static functions just get rid of the "this" keyword in the arguments.
Add keyword static
to class declaration:
// this is a non-generic static class
public static class LinqHelper
{
}
A work-around for people who are experiencing a bug like Nathan:
The on-the-fly compiler seems to have a problem with this Extension Method error... adding static
didn't help me either.
I'd like to know what causes the bug?
But the work-around is to write a new Extension class (not nested) even in same file and re-build.
Figured that this thread is getting enough views that it's worth passing on (the limited) solution I found. Most people probably tried adding 'static' before google-ing for a solution! and I didn't see this work-around fix anywhere else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With