扩展方法与传承 [英] Extension methods versus inheritance

查看:150
本文介绍了扩展方法与传承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有经验,以帮助确定在什么情况下使用的规则?我应该preFER一个比其他大部分时间?

Are there rules of thumb that help determine which to use in what case? Should I prefer one over the other most times?

谢谢!

推荐答案

扩展方法是有用的,但它们是难以发现通过IDE比常规方法中,由于它们没有附加到原始类和有没有线索到了code他们可能驻留。有一些<一href="http://blogs.msdn.com/vbteam/archive/2007/03/10/extension-methods-best-practices-extension-methods-part-6.aspx">best实践建议到哪里把它们以及如何命名他们,但这些都是指导性的,也不能保证有人会跟着他们。

Extension methods are useful, but they are harder to discover through the IDE than regular methods, since they are not attached to the original class and there are no clues as to where the code for them might reside. There are some best practice suggestions as to where to put them and how to name them, but these are only guidelines and there is no guarantee that someone will follow them.

通常你会使用扩展方法,如果你只添加功能众所周知,很好用的类或接口,比如.NET基类,你没有访问到$ C $下。扩展方法也有在约束你不仅要有原装配,你必须有一个与它的扩展方法,必须由消费者您code可以理解的组装。

Usually you would use extension methods if you are only adding functionality to a well known, well used class or interface such as the .Net base classes, that you don't have access to the code for. Extension methods also have the constraint in that you not only have to have the original assembly, you have to have the assembly with the extension methods in it, which must be understood by consumers of your code.

使用继承将允许您添加,删除或覆盖功能,并确保它始终是present带班时,你建立起来。

Using inheritance will allow you to add, remove or override functionality, and ensure that it is always present with the class when you build it.

这篇关于扩展方法与传承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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