English 中文(简体)
推广方法是否是C#的面向目标的特点?
原标题:Are extension methods an object-oriented feature of C#?

推广方法是否遵循了C#中面向目标的模式?

使用推广方法是否是一种良好做法?

在软件开发生命周期中,我们如何在设计阶段审议这一问题?

最佳回答

推广方法不是面向目标的语言。 (与班级、继承、多变等相)

如同每种语文一样,在适宜的情况下,应当加以使用,并用于设计的内容。 已经有几十个问题涉及何时以及如何使用推广方法。

问题回答

Eric Lippert has blobed about this;我怀疑我做的比引述他做的要好得多:

So, yes, the oft-heard criticism that "extension methods are not object-oriented" is entirely correct, but also rather irrelevant. Extension methods certainly are not object-oriented. They put the code that manipulates the data far away from the code that declares the data, they cannot break encapsulation and talk to the private state of the objects they appear to be methods on, they do not play well with inheritance, and so on. They re procedural programming in a convenient object-oriented dress.

They re also incredibly convenient and make LINQ possible, which is why we added them. The fact that they do not conform to some philosophical ideal of what makes an object-oriented language was not really much of a factor in that decision.

然而,我还要指出,它们不仅仅限于准则Q,而且因为同样的原因,它们重新有用in<>。 LINQ。 能够表达具体接口的任意执行方面的算法(例如< 条码>可计算和提数;T> in LINQ to Obhects)。 这种算法通常不会在你重新工作的接口之外有任何背景,因此往往自然不变。

如果你同意,你会采用某种静态通用方法,那么你会使用哪一种yn子?

// Traditional
CollectionUtils.Sort(collection);

// Extension methods
collection.Sort();

我认为,后者更能读懂。 报告简明扼要地阐述了你想要做些什么。 这并不明确how,你希望这样做,但对于 充其量/em>来说,这不太重要,当然,当你重新划定这一特定界线时,更重要的是。

其中有两个部分。

  1. Is it OO when we use it No; it makes you feel that you are calling method on the particular type

  2. 是否根据如何汇编/建立联络处

是的; 编造的代码采用采用推广方法标的静态方法

推广方法只是一种语言特征。 他们从事反对活动,并且是极佳的工具。

视之为扩大班级功能的不同方法。 您可以向以下类别增加新的功能:

  • 加上部分类别声明。 然后,这几类人即时获得一套新的方法和特性。

  • 将名称空间与你的推广方法持有人分类。 之后,这组人又获得了一套新的方法。

相反,组织/语言特征。 不以任何方式打破面向目标的概念。 正如C/C++的负责人/来源档案部门与标的无关,只是一种语言/框架特征。

它取决于。 推广方法只是一种工具。 在适当使用时,这些办法可能非常有用。 但是,如果你用得太大,就会掩盖你的法典。

推广方法只是静态的方法,与某个特定类别或等级的等级相配合。 灰色是OO,但有模块,Ruby有混合体。 我认为这是一种语言特征。 我确信,它仍然是友好的办事处。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!