Delegates and Callbacks

后端 未结 4 948
有刺的猬
有刺的猬 2021-02-03 14:07

Does the term callback in the context of delegates mean ,\"a delegate delegating it works to another delegate inorder to finish some task\" ?

Example :(

4条回答
  •  清歌不尽
    2021-02-03 14:27

    Your example is a good start, but it is incorrect. You don't make a new delegate in the method, it's used in the declaration of an event in the class. See this modified example of your code:

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

    I've only scratched the surface here; search stack overflow for "delegate c#" and "lambda expression c#" for lots more!

提交回复
热议问题