Lig*_*ker 983
我刚认识你,
这很疯狂,
但这是我的号码(代表),
所以如果发生了什么事(事件),
打电话给我,也许(回调)?
Pie*_*ant 78
回调是在进程执行特定任务时将调用的函数.
回调的使用通常是异步逻辑.
要在C#中创建回调,需要在变量中存储函数地址.这是使用一个delegate
或新的lambda语义Func
或Action
.
public delegate void WorkCompletedCallBack(string result);
public void DoWork(WorkCompletedCallBack callback)
{
callback("Hello world");
}
public void Test()
{
WorkCompletedCallBack callback = TestCallBack; // Notice that I am referencing a method without its parameter
DoWork(callback);
}
public void TestCallBack(string result)
{
Console.WriteLine(result);
}
Run Code Online (Sandbox Code Playgroud)
在今天的C#中,这可以使用lambda来完成:
public void DoWork(Action<string> callback)
{
callback("Hello world");
}
public void Test()
{
DoWork((result) => Console.WriteLine(result));
}
Run Code Online (Sandbox Code Playgroud)
ser*_*hio 45
一个回调是作为参数传递给其他代码通过可执行代码.
// Parent can Read
public class Parent
{
public string Read(){ /*reads here*/ };
}
// Child need Info
public class Child
{
private string information;
// declare a Delegate
delegate string GetInfo();
// use an instance of the declared Delegate
public GetInfo GetMeInformation;
public void ObtainInfo()
{
// Child will use the Parent capabilities via the Delegate
information = GetMeInformation();
}
}
Run Code Online (Sandbox Code Playgroud)
Parent Peter = new Parent();
Child Johny = new Child();
// Tell Johny from where to obtain info
Johny.GetMeInformation = Peter.Read;
Johny.ObtainInfo(); // here Johny 'asks' Peter to read
Run Code Online (Sandbox Code Playgroud)
链接
如果您指的是ASP.Net回调:
在ASP.NET网页的默认模型中,用户与页面交互并单击按钮或执行一些导致回发的其他操作.重新创建页面及其控件,页面代码在服务器上运行,并将新版本的页面呈现给浏览器.但是,在某些情况下,从客户端运行服务器代码而不执行回发非常有用.如果页面中的客户端脚本正在维护某些状态信息(例如,本地变量值),则发布页面并获取它的新副本会破坏该状态.此外,页面回发会引入处理开销,这会降低性能并迫使用户等待页面被处理和重新创建.
为了避免丢失客户端状态而不会导致服务器往返的处理开销,您可以编写ASP.NET网页代码,以便它可以执行客户端回调.在客户端回调中,客户端脚本函数将请求发送到ASP.NET网页.网页运行其正常生命周期的修改版本.启动页面并创建其控件和其他成员,然后调用特殊标记的方法.该方法执行您已编码的处理,然后将值返回给浏览器,该值可由另一个客户端脚本函数读取.在整个过程中,页面在浏览器中处于活动状态.
来源:http://msdn.microsoft.com/en-us/library/ms178208.aspx
如果你在代码中引用回调:
回调通常是在特定操作完成或执行子操作时调用的方法的委托.您经常会在异步操作中找到它们.这是一种编程原则,几乎每种编码语言都可以找到它.
更多信息:http://msdn.microsoft.com/en-us/library/ms173172.aspx
小智 6
献给 LightStriker:
示例代码:
class CallBackExample
{
public delegate void MyNumber();
public static void CallMeBack()
{
Console.WriteLine("He/She is calling you. Pick your phone!:)");
Console.Read();
}
public static void MetYourCrush(MyNumber number)
{
int j;
Console.WriteLine("is she/he interested 0/1?:");
var i = Console.ReadLine();
if (int.TryParse(i, out j))
{
var interested = (j == 0) ? false : true;
if (interested)//event
{
//call his/her number
number();
}
else
{
Console.WriteLine("Nothing happened! :(");
Console.Read();
}
}
}
static void Main(string[] args)
{
MyNumber number = Program.CallMeBack;
Console.WriteLine("You have just met your crush and given your number");
MetYourCrush(number);
Console.Read();
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
代码说明:
我创建了代码来实现 LightStriker 在上述回复中提供的有趣解释。我们将委托(编号)传递给方法 ( MetYourCrush
)。如果兴趣(事件)发生在方法 ( MetYourCrush
) 中,那么它将调用持有CallMeBack
方法引用的委托(编号)。因此,该CallMeBack
方法将被调用。基本上,我们通过委托来调用回调方法。
请让我知道,如果你有任何问题。