什么是回调,它在c#中是如何实现的?
当前回答
回调函数是在流程执行完特定任务时调用的函数。
回调的使用通常是在异步逻辑中。
要在c#中创建回调,需要在变量中存储函数地址。这是使用委托或新的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);
}
在今天的c#中,这可以使用lambda来完成:
public void DoWork(Action<string> callback)
{
callback("Hello world");
}
public void Test()
{
DoWork((result) => Console.WriteLine(result));
DoWork(Console.WriteLine); // This also works
}
其他回答
在计算机编程中,回调是作为参数传递给其他代码的可执行代码。 -维基百科:Callback(计算机科学)
c#有为此目的的委托。它们被大量用于事件,因为一个事件可以自动调用许多附加的委托(事件处理程序)。
回调是传递给另一个函数的函数指针。你正在调用的函数将在另一个函数完成时“回调”(执行)。
查看这个链接。
如果您指的是ASP。净回调函数:
In the default model for ASP.NET Web pages, the user interacts with a page and clicks a button or performs some other action that results in a postback. The page and its controls are re-created, the page code runs on the server, and a new version of the page is rendered to the browser. However, in some situations, it is useful to run server code from the client without performing a postback. If the client script in the page is maintaining some state information (for example, local variable values), posting the page and getting a new copy of it destroys that state. Additionally, page postbacks introduce processing overhead that can decrease performance and force the user to wait for the page to be processed and re-created. To avoid losing client state and not incur the processing overhead of a server roundtrip, you can code an ASP.NET Web page so that it can perform client callbacks. In a client callback, a client-script function sends a request to an ASP.NET Web page. The Web page runs a modified version of its normal life cycle. The page is initiated and its controls and other members are created, and then a specially marked method is invoked. The method performs the processing that you have coded and then returns a value to the browser that can be read by another client script function. Throughout this process, the page is live in the browser.
来源:http://msdn.microsoft.com/en-us/library/ms178208.aspx
如果你在代码中引用回调:
回调通常是对方法的委托,在特定操作完成或执行子操作时调用这些方法。您将经常在异步操作中发现它们。这是一种在几乎所有编码语言中都可以找到的编程原则。
更多信息请访问:http://msdn.microsoft.com/en-us/library/ms173172.aspx
对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();
}
}
代码的解释:
我创建了代码来实现LightStriker在上面一个回复中提供的有趣解释。我们正在传递委托(数字)到一个方法(MetYourCrush)。如果感兴趣(事件)发生在方法(MetYourCrush)中,那么它将调用持有CallMeBack方法引用的委托(数字)。CallMeBack方法会被调用。基本上,我们传递委托来调用回调方法。
如果你有任何问题,请告诉我。
可能不是字典定义,但回调通常是指一个函数,它位于特定对象的外部,被存储,然后在特定事件时调用。
例如,当创建一个UI按钮时,它存储了对执行某个操作的函数的引用。该操作由代码的不同部分处理,但当按下按钮时,将调用回调,这将调用要执行的操作。
c#使用“事件”和“委托”而不是“回调”这个术语,你可以在这里找到更多关于委托的信息。
推荐文章
- 拒绝访问该路径
- Visual Studio - Resx文件默认“内部”为“公共”
- 使用linq转换列表到字典,不用担心重复
- 单元测试:日期时间。现在
- 什么是回调?
- 返回匿名类型的结果?
- 何时使用IList,何时使用List
- ConfigurationManager。AppSettings在.NET Core 2.0中可用?
- 在c#的控制台应用程序中使用'async
- 在单元测试中设置HttpContext.Current.Session
- 如何开始开发Internet Explorer扩展?
- 更新行,如果它存在,否则插入逻辑实体框架
- 在什么情况下SqlConnection会自动被征召到环境事务范围事务中?
- 用c#解析JSON
- Windows窗体中的标签的换行