代表和回调

use*_*677 6 c# callback

在委托的上下文中,术语回调是否意味着" 代理委托它为另一个代表工作以完成某项任务 "?

示例:( 根据我的理解,我已经实现了回调,如果错误则纠正我)

namespace Test
{
    public delegate string CallbackDemo(string str);  

    class Program
    {
        static void Main(string[] args)
        {
            CallbackDemo handler = new CallbackDemo(StrAnother);
            string substr = Strfunc(handler);
            Console.WriteLine(substr);
            Console.ReadKey(true);
        }

        static string Strfunc(CallbackDemo callback)
        {
           return callback("Hello World");   
        }

        static string StrAnother(string str)
        {
            return str.Substring(1, 3).ToString();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请根据需要提供示例.

RCI*_*CIX 11

你的例子是一个好的开始,但它是不正确的.您不在方法中创建新委托,它在类的事件声明中使用.查看代码的这个修改示例:

namespace Test
{
    //In this case, this delegate declaration is like specifying a specific kind of function that must be used with events.
    public delegate string CallbackDemo(string str);
    class Program
    {
        public static event CallbackDemo OnFoobared;
        static void Main(string[] args)
        {
            //this means that StrAnother is "subscribing" to the event, in other words it gets called when the event is fired
            OnFoobared += StrAnother;
            string substr = Strfunc();
            Console.WriteLine(substr);
            Console.ReadKey(true);
            //this is the other use of delegates, in this case they are being used as an "anonymous function". 
            //This one takes no parameters and returns void, and it's equivalent to the function declaration 
            //'void myMethod() { Console.WriteLine("another use of a delegate"); }'
            Action myCode = delegate
            {
                Console.WriteLine("another use of a delegate");
            };
            myCode();
            Console.ReadKey(true);
            //the previous 4 lines are equivalent to the following however this is generally what you should use if you can
            //its called a lambda expression but it's basically a way to toss arbitrary code around
            //read more at http://www.developer.com/net/csharp/article.php/3598381/The-New-Lambda-Expressions-Feature-in-C-30.htm or 
            //http://stackoverflow.com/questions/167343/c-lambda-expression-why-should-i-use-this
            Action myCode2 = () => Console.WriteLine("a lambda expression");
            myCode2();
            Console.ReadKey(true);
        }

        static string Strfunc()
        {
            return OnFoobared("a use of a delegate (with an event)");
        }
        static string StrAnother(string str)
        {
            return str.Substring(1, 3).ToString();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是在这里划过表面; 搜索堆栈溢出为"委托c#"和"lambda表达c#"更多!


小智 5

回调是委托在调用时执行的内容。例如,当通过委托使用异步模式时,您将执行以下操作:

public static void Main(string[] args)
{
    Socket s = new Socket(...);

    byte[] buffer = new byte[10];
    s.BeginReceive(buffer, 0, 10, SocketFlags.None, new AsyncCallback(OnMessageReceived), buffer);

    Console.ReadKey();
}

public static void OnMessageReceived(IAsyncResult result)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

OnMessageReceived是回调,是通过调用委托执行的代码。有关更多信息,请参见此Wikipedia文章;有关其他示例,请参阅Google。