gpt4 book ai didi

c# - 嵌套在静态类中的类中的扩展方法

转载 作者:行者123 更新时间:2023-11-30 16:51:38 25 4
gpt4 key购买 nike

我知道我们不能在嵌套类中使用扩展方法,并且该选择背后的原因非常有道理,我不是来谈论这种情况的。

是否有设计原因导致无法在嵌套在 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;
}
}
}

是否仅仅因为它是一个需要更多工作的“极端情况”并且被认为是无用的?还是这背后有其他设计原因?

最佳答案

在此answer , Eric Lippert 认为

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.

他接着说

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.

但是,您可以提交功能请求 here .

关于c# - 嵌套在静态类中的类中的扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33674094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com