本文介绍了代表VS接口在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只要到提出这个问题,因为我想目前挖掘到的使用和代表们的目的,虽然它很可能已经在类似的配方被问。

I would like to pose this question as long as I am trying currently to dig into the use and the purpose of delegates, although it is likely to have been asked in similar formulations.

我知道代表作为在C ++中使用函数指针。事实的事项是,如果在C#它们所服务大多作为替代接口与多态。因为我可以创建一个特定类的子类,并提供给他们适当的方法来每个人,有什么额外提供给代表?有没有规定其使用或仅仅是提高了代码的可维护性时使用委托个案?你会推荐他们对接口进行广泛部署?

I know that delegates serve as function pointers used in C++. The matter of the fact is if in C# they serve mostly as an alternative to interfaces and polymorphism. Since I can create subclasses of a specific class and supply them the appropriate methods to each one, what offer delegates additionally to that? Are there cases that stipulate their use or is merely the maintainability of the code improved when delegates used? Would you recommend their wide deployment over interfaces?

我只说关于代表,我想区分事件的角色的作用。

I am speaking solely about delegates and I want to distinguish their role from the events role.

推荐答案

是的,代表们在如单方法接口许多方面。但是:

Yes, delegates are in many ways like single-method interfaces. However:


  • 有内置的CLR他们

  • 支持
  • 有一个在该框架支持其中,包括多播能力和异步调用

  • 有一个在方法组转换,Lambda表达式,匿名方法

  • 的形式额外的C#/ VB语言支持他们规定的事件(即事件和委托是一种匹配对)
  • 他们的意思是你的的需要实现的接口,你要创建的每个委托实例一个单独的类

  • There is support built into the CLR for them
  • There's support in the framework for them, including multi-cast abilities and asynchronous invocation
  • There's additional C#/VB language support in the form of method group conversions, lambda expressions, anonymous methods
  • They're mandated for events (i.e. events and delegates are a sort of matching pair)
  • They mean you don't need to implement an interface in a separate class for each delegate instance you want to create.

最后一点也是最重要的 - 考虑的LINQ表达式:

The last point is the most important one - consider a LINQ expression of:

var query = collection.Where(x => x > 5)
                      .Select(x => x * x);

现在想象一下,如果以表达的逻辑X> 5 X * X 你必须写每个表达式一个单独的类,并实现一个接口:克鲁夫特的VS有用的代码量会荒谬。现在当然语言的可能的已被设计为允许从lambda表达式转换成通过单独的类接口实现,但你还是会失去了能够简单地编写一个单独的方法在互利,共创与作为目标委托。你也还是会输的多播能力。

Now imagine if to express the logic of x > 5 and x * x you had to write a separate class for each expression, and implement an interface: the amount of cruft vs useful code would be ridiculous. Now of course the language could have been designed to allow conversions from lambda expressions into interface implementations via separate classes, but then you'd still lose the benefit of being able to simply write a separate method and create a delegate with that as the target. You'd also still lose the multi-cast abilities.

由于类似的想法exercsise,考虑循环语句,如,而。难道我们真的需要的他们,当我们已经有了转到?不。但生活要好得多的的他们。同样是代表真实的 - 而事实上属性,事件等,他们都使开发更简单

As a similar thought exercsise, consider looping statements such as while and for. Do we really need them when we've got goto? Nope. But life is much better with them. The same is true of delegates - and indeed properties, events, etc. They all make the development simpler.

这篇关于代表VS接口在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:02