为什么扩展方法只能在非嵌套,非通用类的静态允许吗? [英] Why are extension methods only allowed in non-nested, non-generic static class?

查看:94
本文介绍了为什么扩展方法只能在非嵌套,非通用类的静态允许吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要扩展方法只能在非嵌套,非通用类的静态允许吗?
它是无用的考虑嵌套,通用的静态类扩展方法?


解决方案

  

为什么要扩展方法只能在非嵌套,非通用类的静态允许吗?


由于PRATIK所指出的,我们面临的问题不是为什么扩展方法的的允许嵌套或泛型类?我们面临的语言设计者的问题是为什么的的扩展方法进行嵌套或泛型类允许吗?

除非该功能是由一些真实世界的用户需要正当的,我们不会承担设计的相当大的成本,实现,测试,记录和维护功能。

基本上,设计扩展方法,使LINQ的工作。凡是没有作出贡献,使LINQ的工作被切断。 LINQ只需要在静态,非通用的,非嵌套类工作扩展方法,所以这就是我们设计和实现。

如果你有这样一个场景,扩展方法将成为非静态的,通用的,或嵌套类有用的话,我很乐意看一看的情况。更真实场景,我们得到,就越有可能的是,我们要做出特色以有利于这些情况,一些假想的未来的语言。


  

时的无用考虑嵌套,通用的静态类扩展方法?


没有,这是一个伟大的想法,考虑它。我们将在我们的玩忽职守如果我们不考虑。我们仔细考虑了很长的时间,并决定,审议的基础上,做了功能的成本没有被累积的效益合理的。

Why are extension methods only allowed in non-nested, non-generic static class? Is it useless to consider extension methods in nested, 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.

这篇关于为什么扩展方法只能在非嵌套,非通用类的静态允许吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆