IEatBagels
IEatBagels

Reputation: 843

Extension method in a class that's nested in a static class

I know we can't have extension method in nested classes and the reasons behind that choice makes perfect sense, I'm not here to talk about this case.

Is there a design reason why it's impossible to have an extension method in a class that is nested in a static class?

public static class MyClass1
{
    public static class MyClass2
    {
        public static int Add(this int a, int b) //Doesn't compile
        {
            return a + b;
        }
    }
}

Is it just because it's a "corner case" that required more work and was considered useless? Or is there another design reason behind this?

Upvotes: 3

Views: 360

Answers (1)

Amadeus Sanchez
Amadeus Sanchez

Reputation: 2565

In this answer, Eric Lippert argues that

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.

He goes on to say that

We considered it carefully for a long time and decided that on the basis of that consideration, the costs of doing the feature (extension methods in nested, generic static class) were not justified by the benefits accrued.

However, you can submit feature requests here.

Upvotes: 2

Related Questions