.net 委托和回调是否相同或相似?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/290819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are delegates and callbacks the same or similar?
提问by
Are delegates the same thing as callbacks? Or are they related somehow?
委托和回调是一回事吗?或者他们有什么关系?
回答by Charles Bretana
A "callback" is a term that refers to a coding design pattern, available in any language that has function pointers, or an analogue to function pointers (which is kinda what a delegate is)
“回调”是一个术语,指的是一种编码设计模式,可以在任何具有函数指针的语言中使用,或者类似于函数指针(这有点像委托)
In this pattern, you pass a pointer to a function to another function, so that within the called function, it can "call back" the function you passed to it. In this way you can control a large chunk of the internal behavior of a method from outside the method, by passing pointers to different "callback" function each time you call it... An example of a callback is when you have a sorting algorithm that has to be passed a pointer to a function that will "compare" any arbitrary pair of objects in the list to be sorted, to determine which goes before the other. On one call to the sort method, you might pass a callback function that compares by object name, and another time pass a function that compares by object weight, or whatever...
在这种模式中,您将一个函数的指针传递给另一个函数,以便在被调用的函数中,它可以“回调”您传递给它的函数。通过这种方式,您可以从方法外部控制方法的大部分内部行为,方法是每次调用时将指针传递给不同的“回调”函数......回调的一个例子是当你有一个排序算法时必须传递一个指向函数的指针,该函数将“比较”要排序的列表中的任意一对对象,以确定哪个在另一个之前。在对 sort 方法的一次调用中,您可能会传递一个按对象名称进行比较的回调函数,而另一次则传递一个按对象权重进行比较的函数,或者其他任何...
A Delegate, otoh, is a specific .Net type that acts as a signature-specific container for a function pointer... In .Net managed code, delegates are the only way to create and use a function pointer. So in .Net to do a callback, you are in fact passing a delegate... But delegates can be used in other scenarios besides callbacks. (specifically, delegates can be also used to implement multi-threading from the .Net thread pool)
委托,otoh,是一种特定的 .Net 类型,它充当函数指针的签名特定容器......在 .Net 托管代码中,委托是创建和使用函数指针的唯一方法。所以在 .Net 中做回调,其实你是在传递一个委托......但是除了回调之外,委托还可以用于其他场景。(具体来说,delegate 也可以用于从 .Net 线程池中实现多线程)
Callbacks are also used to implement the "observer" pattern (implemented in .Net using Events, which are themselves a special type of delegate)
回调也用于实现“观察者”模式(在 .Net 中使用事件实现,事件本身是一种特殊类型的委托)
回答by Jon Skeet
(I assume you're talking about .NET here. If not, please elaborate.)
(我假设您在这里谈论的是 .NET。如果不是,请详细说明。)
Delegates are the idiomatic way of implementing callbacks in .NET - but you don't have to. You could use an interface, for example. (In particular you could then have one callback with one method to call on error, and one on success. Of course, you could take two delegates instead...)
委托是在 .NET 中实现回调的惯用方式 - 但您不必这样做。例如,您可以使用接口。(特别是,你可以有一个回调,一种方法在错误时调用,一种方法在成功时调用。当然,你可以用两个委托代替......)
There are plenty of uses for delegates beyond callbacks in .NET - it depends on exactly what you deem to be a callback, but GUI event handlers, thread-starters, filters and projections (and more!) in LINQ to Objects all use delegates.
除了 .NET 中的回调之外,委托还有很多用途——这完全取决于您认为什么是回调,但 LINQ to Objects 中的 GUI 事件处理程序、线程启动器、过滤器和投影(以及更多!)都使用委托。
回答by Wolf5
They are hand in hand related. A delegate is the description on how the callback function looks like:
他们是手牵手的关系。委托是对回调函数外观的描述:
delegate void MyDelegate(string Text);
Then you can have a function that can take in the callback as a parameter.
然后,您可以拥有一个可以将回调作为参数接收的函数。
//This will result in a MessageBox with "Lalalala"
MyFunctionThatGetsTheCallbackFunctionRef(MyCallBackFunc);
void MyFunctionThatGetsTheCallbackFunctionRef(MyDelegate TheFunction){
TheFunction("Lalalala");
}
void MyCallBackFunc(string Text){
//my callback
MessageBox.Show(Text);
}
回答by Steven A. Lowe
generically, a delegate is an object used to access a method external to the object owning the method, while a callback is a variable that holds a delegate
一般而言,委托是用于访问拥有该方法的对象外部的方法的对象,而回调是持有委托的变量
in C#, the terms are used interchangeably
在 C# 中,这些术语可以互换使用
回答by Watachiaieto
The service class has a variable function called a delegate. The calling class has a preferred function that it wants run called a callback.
服务类有一个称为委托的可变函数。调用类有一个首选函数,它希望运行称为回调。
The calling class sets the delegate to it's callback.
调用类将委托设置为它的回调。
The difference between a delegate and a callback is the perspective: The service class calls the delegate that is set to the calling classes callback.
委托和回调之间的区别在于视角:服务类调用设置为调用类回调的委托。

