Why Are Extension Methods Only Allowed in Non-Nested, Non-Generic Static Class

Why are extension methods only allowed in non-nested, non-generic static class?

Why are extension methods only allowed in non-nested, non-generic static class?

As Pratik points out, the question we face is not "why are extension methods not allowed in nested or generic classes?" The question we face as language designers is "why should extension methods be allowed in nested or generic classes?"

Unless the feature is justified by some real-world user need, we're not going to take on the considerable costs of designing, implementing, testing, documenting and maintaining the feature.

Basically, extension methods were designed to make LINQ work. Anything that didn't contribute to making LINQ work was cut. LINQ only needs extension methods in static, non-generic, non-nested classes to work, so that's what we designed and implemented.

If you have a scenario where extension methods would be useful in non-static, generic, or nested classes then I'm happy to take a look at the scenario. The more real-world scenarios we get, the more likely it is that we'll make a feature in some hypothetical future language that benefits those scenarios.

Is it useless to consider extension methods in nested, generic static class?

No, it is a great idea to consider it. We would be remiss in our duties if we did not consider it. We considered it carefully for a long time and decided that on the basis of that consideration, the costs of doing the feature were not justified by the benefits accrued.

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

change

public class LinqHelper

to

public static class LinqHelper

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

  1. The class which defines an extension method must be non-generic, static and non-nested
  2. Every extension method must be a static method
  3. The first parameter of the extension method should use the this keyword.

Why must C# extension methods be defined in static classes?

This is more of an observation than an answer, but...

When you call an instance method, a reference to the object you are calling is pushed onto the stack as the first argument in your method call. That first argument is "this" and is done implicitly.

When you define an extension method, you explicitly define a "this" as the first argument.

Is it possible that method resolution would be confusing if you could define extension methods and instance methods in the same class i.e. defining methods with the same name and, in effect, the same parameters when the "this" parameter is included.

Extension method must be defined in non-generic static class

If you remove "this" from your parameters it should work.

public static IChromosome To<T>(this string text)

should be:

public static IChromosome To<T>(string text)

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

As the error states, extension methods can only be declared on a non-generic static class. You are attempting to declare the FindMissing method in the Missing class, which is not a non-generic static class.

You have two options:

  1. Make the method a normal method, in which case it can stay in the Missing class
  2. Declare another class, perhaps MissingExtensions, to contain the method

This is what the second option would look like:

public static class MissingExtensions
{
public static IEnumerable<int> FindMissing(this List<int> list)
{
// ...
}
}

Extension method is not working,If method in nonstatic class?

Please tell why its not working in nonstatic class.

Because that's just how extension methods are specified. They must be declared in a non-nested, non-generic class.

From section 10.6.9 of the C# 5 specification:

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.

Why do you want to declare it in a non-static class? What are you trying to achieve which can't be achieved equally well using a static class? (I can just about imagine some possibilities, but they're not things I've ever wanted to do in extension methods myself...)



Related Topics



Leave a reply



Submit