我在哪里使用代表?[英] Where do I use delegates?

本文是小编为大家收集整理的关于我在哪里使用代表?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

有哪些真正需要代表的现实世界?我很好奇这种方法是最佳解决方案的情况.无需代码.

推荐答案

委托是定义特定方法的命名类型.就像一个类别定义为其定义的给定类型对象的所有成员列出一样,代表们为其定义的方法签署了方法签名.

基于此语句,委托是一个函数指针,它定义了该函数的外观.

代表真实世界应用的一个很好的例子是 a>.在链接的示例中,您会注意到 array "> array.find.find 要搜索的数组,然后是一个谓词来处理要找到的标准.在这种情况下,它传递了与谓词签名匹配的方法productgt10.

其他推荐答案

:掌握C#3.0"

的基本原理

一般场景:当国家元首死亡时,美国总统通常没有时间参加葬礼 亲自.相反,他派遣了一名代表.这个代表通常是 副总统,但有时副总裁不可用, 总统必须派遣其他人,例如国务卿或 甚至第一夫人.他不想"硬化"他的委派 一个人的权力;他可能会将这一责任委托给 任何能够执行正确国际协议的人.

总统事先定义了将委派什么责任 (参加葬礼),将通过哪些参数(慰问, 善意的话),以及他希望回来的价值(好意志).然后他 将一个特定的人分配给该委派的责任 随着总统职位的进展,"运行时间".

在编程方案中:您经常面对需要执行特定动作的情况,但您不知道 提前哪种方法,甚至哪个对象,您需要调用 执行它.

例如:一个按钮可能不知道需要通知哪些对象或对象.而不是将按钮接线到特定 对象,您将将按钮连接到委托,然后解决 该程序执行时,该委派给特定方法.

其他推荐答案

代表对通用列表的一种常见用途是通过Action代表(或其匿名等效的)来创建单行操作:

myList.Foreach( i => i.DoSomething());

我还发现谓词委托在搜索或修剪列表中非常有用:

myList.FindAll( i => i.Name == "Bob");    
myList.RemoveAll( i => i.Name == "Bob");

我知道您说的无需代码,但是我发现通过代码表达其有用性会更容易. :)

本文地址:https://www.itbaoku.cn/post/627478.html

问题描述

What are some real world places that call for delegates? I'm curious what situations or patterns are present where this method is the best solution. No code required.

推荐答案

A delegate is a named type that defines a particular kind of method. Just as a class definition lays out all the members for the given kind of object it defines, the delegate lays out the method signature for the kind of method it defines.

Based on this statement, a delegate is a function pointer and it defines what that function looks like.

A great example for a real world application of a delegate is the Predicate. In the example from the link, you will notice that Array.Find takes the array to search and then a predicate to handle the criteria of what to find. In this case it passes a method ProductGT10 which matches the Predicate signature.

其他推荐答案

As stated in "Learning C# 3.0: Master the fundamentals of C# 3.0"

General Scenario: When a head of state dies, the President of the United States typically does not have time to attend the funeral personally. Instead, he dispatches a delegate. Often this delegate is the Vice President, but sometimes the VP is unavailable and the President must send someone else, such as the Secretary of State or even the First Lady. He does not want to “hardwire” his delegated authority to a single person; he might delegate this responsibility to anyone who is able to execute the correct international protocol.

The President defines in advance what responsibility will be delegated (attend the funeral), what parameters will be passed (condolences, kind words), and what value he hopes to get back (good will). He then assigns a particular person to that delegated responsibility at “runtime” as the course of his presidency progresses.

In programming Scenario: You are often faced with situations where you need to execute a particular action, but you don’t know in advance which method, or even which object, you’ll want to call upon to execute it.

For Example: A button might not know which object or objects need to be notified. Rather than wiring the button to a particular object, you will connect the button to a delegate and then resolve that delegate to a particular method when the program executes.

其他推荐答案

One common use of delegates for generic Lists are via Action delegates (or its anonymous equivalent) to create a one-line foreach operation:

myList.Foreach( i => i.DoSomething());

I also find the Predicate delegate quite useful in searching or pruning a List:

myList.FindAll( i => i.Name == "Bob");    
myList.RemoveAll( i => i.Name == "Bob");

I know you said no code required, but I find it easier to express its usefulness via code. :)