2025-05-12 05:00:00

什么是回调?

什么是回调,它在c#中是如何实现的?


当前回答

回调是传递给另一个函数的函数指针。你正在调用的函数将在另一个函数完成时“回调”(执行)。

查看这个链接。

其他回答

回调是传递给另一个函数的函数指针。你正在调用的函数将在另一个函数完成时“回调”(执行)。

查看这个链接。

我才刚认识你, 这很疯狂, 这是我的number (delegate) 所以如果发生了什么(事件), 打给我吧(回电话)?

可能不是字典定义,但回调通常是指一个函数,它位于特定对象的外部,被存储,然后在特定事件时调用。

例如,当创建一个UI按钮时,它存储了对执行某个操作的函数的引用。该操作由代码的不同部分处理,但当按下按钮时,将调用回调,这将调用要执行的操作。

c#使用“事件”和“委托”而不是“回调”这个术语,你可以在这里找到更多关于委托的信息。

定义

回调是可执行的代码 作为参数传递给其他代码。

实现

// 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();
    }
}

使用

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

链接

c#的更多细节。

回调是作为参数传递给另一个函数的函数。这种技术允许函数调用形参函数实参,甚至将值传递回调用方。回调函数可以设计为在函数结束之前/之后运行,并且可以传递一个值。

它是一种构造,你调用一个长时间运行的函数,并要求他在它完成后回调你,可以返回一个参数结果给调用者。

这就像有人在你工作的时候打电话给你询问状态,你说“你知道吗,给我5分钟,我会给你回电”,最后你打电话给他更新情况。如果你是一个函数,调用者只是添加并传递了另一个你在最后调用的函数。这可以简单地用c#编写为:

public void VinodSrivastav(Action statusUpdate){
    //i am still here working..working

    //i have finished, calling you
    statusUpdate();
}

//invokes
stackoverflow.VinodSrivastav((cam) => { 
    Console.Write("Is it finished"); 
});

一个简单的例子是迭代器函数,其中的return将是多次,可以认为我们为它设置了yield:

public void IntreationLoop(int min, int max,Action<int> Callback)
{
    for(int i = min;i<= max;i++)
        Callback(i);
}

//call
IntreationLoop(5,50,(x) => { Console.Write(x); }); //will print 5-50 numbers

在上面的代码中,函数返回类型为void,但它有一个Action<int>回调函数,该回调函数被调用,并将循环中的每个项发送给调用者。

同样的事情也可以用if..else或try..catch block as:

public void TryCatch(Action tryFor,Action catchIt)
{
    try{
        tryFor();
    }
    catch(Exception ex)
    {
        Console.WriteLine($"[{ex.HResult}] {ex.Message}");
        catchIt();
    }
}

称之为:

TryCatch(()=>{ 
        int r = 44;
        Console.WriteLine("Throwing Exception");
        throw new Exception("something is wrong here");
    }, ()=>{
        Console.WriteLine("It was a mistake, will not try again");
    });

在2022年,我们有Func和Action做同样的事情,请参阅下面的演示代码,其中展示了如何使用它:

void Main()
{
    var demo = new CallbackDemo();
    demo.DoWork(()=> { Console.WriteLine("I have finished the work"); });
    demo.DoWork((r)=> { Console.WriteLine($"I have finished the work here is the result {r}"); });
    demo.DoWork(()=> { Console.WriteLine($"This is passed with func"); return 5;});
    demo.DoWork((f)=> { Console.WriteLine($"This is passed with func and result is {f}"); return 10;});
    
}

// Define other methods and classes here
public class CallbackDemo
{

    public void DoWork(Action actionNoParameter)
    {
        int a = 5;
        int b = 10;
        
        //i will do th maths and call you back
        int result = a + b;
        
        //callback
        actionNoParameter(); //execute
        Console.WriteLine($"[The Actual Result is {result}]");
    }
    
    public void DoWork(Action<int> actionWithParameter)
    {
        int a = 5;
        int b = 10;
        
        //i will do th maths and call you back
        int result = a + b;
        
        //callback
        actionWithParameter(result); //execute
        Console.WriteLine($"[The Actual Result is {result}]");
    }
    
    public void DoWork(Func<int> funcWithReturn)
    {
        int a = 5;
        int b = 10;
        
        //i will do th maths and call you back
        int result = a + b;
        
        //callback
        int c = funcWithReturn(); //execute
        
        result += c;
        Console.WriteLine($"[The Actual Result is {result}]");
    }
    
    public void DoWork(Func<int,int> funcWithParameter)
    {
        int a = 5;
        int b = 10;
        
        //i will do th maths and call you back
        int result = a + b;
        
        //callback
        result += funcWithParameter(result); //execute
        Console.WriteLine($"[The Actual Result is {result}]");
    }
}