为什么不支持C#静态类扩展方法? [英] Why aren't C# static class extension methods supported?

查看:794
本文介绍了为什么不支持C#静态类扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从<一知道href=\"http://stackoverflow.com/questions/249222/can-i-add-extension-methods-to-an-existing-static-class\">this问题是扩展方法只能在类实例上运行,而不是静态的类本身。这意味着我不能延长等有用静态类转换数学

我想知道的是,为什么是这样?从上面的链接,对于如何在C#团队可能已经实现了这种功能提出了一些建议。有一些哲学理由不支持它?

例如,这里有一个 的理由背后为什么没有内置的LINQ 的ForEach&LT; T&GT; 延长的IEnumerable&LT; T&GT;


解决方案

  

C#团队可能已经实现了这种功能。有一些哲学理由不支持它?


有没有技术上的原因,也没有哲学道理。然而,正如我经常指出,我没有为的的提供的理由做一个功能。特点是不便宜;他们是非常昂贵的,他们不仅要证明自己的成本,他们必须证明如果不这样做,我们的一百其他功能的机会成本可能的都与预算完成。我们必须证明的功能,我们的利益相关成本,但我们不必辜负省时省力的的实施不符合我们的酒吧功能。

在具体地,所提出的特征不执行任何对LINQ;添加扩展方法,使LINQ的工作。凡是没有做LINQ的工作是很难进入C#3.0;我们有很多工作时间表并没有太多的时间做这件事。(我很惊讶,自动属性中做到了。)甚至设计它节省了大量的时间和精力被其他花前切割不必要的功能事情的的制作LINQ的工作。

在短:建议的功能从未见过我们的酒吧超过成本的净效益,而且我们总是不得不花费我们有限的时间和精力放在更重要的功能。

I know from this question that extension methods can only operate on class instances, not the static class itself. This means I can't extend useful static classes like Convert and Math.

What I want to know is, why is this the case? From the link above, there are some suggestions on how the C# team could have implemented this kind of functionality. Is there some philosophical reason why it isn't supported?

For example, here's a rationale behind why there is no built-in LINQ ForEach<T> extension for IEnumerable<T>.

解决方案

the C# team could have implemented this kind of functionality. Is there some philosophical reason why it isn't supported?

There's no technical reason, and no philosophical reason. However, as I often point out, I don't have to provide a justification for not doing a feature. Features aren't cheap; they are extremely expensive and they must not only justify their own cost, they must justify the opportunity cost of not doing the hundred other features we could have done with that budget. We must justify the cost of features to our stakeholders, but we need not justify saving time and effort by not implementing features that don't meet our bar.

In particular, the proposed feature does nothing for LINQ; extension methods were added to make LINQ work. Anything that didn't make LINQ work was very hard to get into C# 3.0; we had a lot of work on the schedule and not much time to do it in. (I was surprised that automatic properties made it in.) Cutting an unnecessary feature before even designing it saved a lot of time and effort that was spent on other things that do make LINQ work.

In short: the suggested feature has never met our bar for net benefit over cost, and we've always had more important features to spend our limited time and effort on.

这篇关于为什么不支持C#静态类扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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