委托比通过类的对象调用普通函数需要什么? [英] What is the need of delegate than calling the normal function via object of the class ?

查看:88
本文介绍了委托比通过类的对象调用普通函数需要什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有2个按钮。添加& Substract。

我将写两个单独的函数Add& Substract&我会在每次点击按钮时给他们打电话。

那么,为什么我事先已经知道哪些功能可以调用哪个按钮点击,我为什么要去代表呢?



我尝试了什么:



我试过通过正常的函数调用&也可以通过代理实施。

但是我在使用委托超过正常函数调用时没有区别。

lets' say I have 2 buttons. Add & Substract.
I will write two seperate functions "Add" & "Substract" & i will call them accordingly on each button click.
Then why should I go for delegate when things are already known to me in advance which function to call on which button click ?

What I have tried:

I have tried implementing it via both normal function calling & also by implementing via delegate.
But i did not get the difference when to use delegate over normal function call.

推荐答案

代理不仅用于调用这个或那个 - 他们可以提供一个被称为的方法链。你已经使用了代表,相当多 - 你只是没有意识到它,因为它们被隐藏在事件处理的核心!



代表的一个原因基于它的方法是它的灵活性:你不必编写工作的代码来调用哪种方法:

Delegates are used for more than just "call this or that" - they can provide a "chain of methods to be called" for example. You use delegates already, quite a bit - you just don't realize it because they are "hidden" at the core of Event handling!

One reason for a delegate based approach is it's flexibility: you don;t have to write code which "works out" which method to call:
private DoIt(MyEnum functionType)
   {
   switch(functionType)
      {
      case MyEnum.Add: Add(); break;
      case MyEnum.Sub: Sub(); break;
      default: throw new ParameterException("Unknown function type: " + functionType);
      }
   }




private DoIt(MyDelegate del)
   {
   if (del != null)
      {
      del();
      }
   }



当您添加Mult和Div方法时,您根本不必更改DoIt中的代码 - 您只需通过适当的委托,一切都透明地工作。



一个例子是Sort方法,当你传递比较委托来比较UserNames,或者一个不同的委托来比较BirthDates取决于您想要排序的内容 - 如果方法匹配委托签名并返回正确的值(-1,0或+1),排序将起作用而不关心它实际比较的内容!


When you add Mult and Div methods, you don't have to change the code in DoIt at all - you just pass the appropriate delegate and it all works transparently.

One example of this is a Sort method, when you pass a comparison delegate to compare UserNames, or a different delegate to compare BirthDates depending on what you want to sort - provided the methods match the delegate signature and return the right value (-1, 0, or +1) the sort will work without caring what it it actually comparing!


你的逻辑严重,严重缺陷。你可以从定义上没有任何本质区别的那一点开始,然后在此基础上得出没有区别的结论。



如果你仍然没有得到它,我将在另一个例子中解释。这就像将显微镜看作钉子锤击装置一样,但有许多未知目的。这是您的起点。然后你拿它,敲钉子,并得出结论,如果功能与悍马相同,并且在这个过程中没有使用未知部分,那么应该提出一个问题,它们不是没用吗?......这是确切地说你做了什么。



实际上,委托是一个非常深刻的概念的实现之一: 方法作为第一类对象 的。委托是一个对象,与方法不同,它的实例可以作为方法参数或类型成员引用,属性或字段传递。这样,这是另一种抽象设备,以及方法参数传递或变量。这就是它在维基百科中的解释:

https://en.wikipedia.org/ wiki / Delegation_%28programming%29 [ ^ ],

一等公民 - 维基百科,免费百科全书 [< a href =https://en.wikipedia.org/wiki/First-class_citizentarget =_ blanktitle =New Window> ^ ]。



我不能因为不理解.NET代理而责怪你。没有多少开发者了解他们的全部容量不幸的是,这是因为MSDN文档在大多数部分都是全面的,对委托和委托实例留下了很多谜团。关于这个主题的帮助页面更接近一些烹饪书的配方并留下重要的功能,忘记了机制,在幕后。

我的文章的一部分描述了它的大部分内容: 动态方法Dispather, 4.1。关于代表实例的性质



-SA
Your logic is badly, badly flawed. You start from the point where there should not be any essential difference by definition, and then, based on that, draw a conclusion that there is no difference.

If you still did not get it, I'll explained on another example. This is like considering a microscope as a device for hammering in nails, but with a number of part of unknown purpose. This is your starting point in first place. Then you take it, hammer a nail, and conclude that if functions the same way as the hummer, and the unknown parts are not utilized in the process, so a question should be raised, "aren't they useless?"… This is exactly what you have done.

Actually, the delegate is one of the implementations of a very deep conception: methods as first-class objects. A delegate is an object, and unlike a method, its instance can be passed as a method argument or the type member reference, a property or a field. This way, this is another device of abstraction, along with, say, method parameter-passing or variable. This is how it explained in Wikipedia:
https://en.wikipedia.org/wiki/Delegation_%28programming%29[^],
First-class citizen — Wikipedia, the free encyclopedia[^].

I cannot blame you much in not understanding .NET delegates, specifically. Not many developers understand their full capacity. Unfortunately, this is because MSDN documentation, comprehensive in most parts, leave a lot of mystery about delegates and delegate instances; its help pages on the topic are closer to some cook-book recipes and leave important functionality, forget the mechanism, behind the scene.
Big part of it is described in one section of my article: Dynamic Method Dispather, 4.1. On the Nature of Delegate Instance.

—SA


这篇关于委托比通过类的对象调用普通函数需要什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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