Does the term callback in the context of delegates mean ,"a delegate delegating it works to another delegate inorder to finish some task" ?
Example :(Based on my understanding,I have implemented a callback,correct me if it is wrong)
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();
}
}
}
Please provide examples as necessary.
Callbacks are similar in function to the delegate pattern. They do the same thing: letting other objects know when something happened, and passing data around. What differentiates them from the delegate pattern, is that instead of passing a reference to yourself, you are passing a function.
Delegate is a good way to implement Callback. But, you could use Interface for this. Because, suppose you have two methods - one for the success and another for the error and these methods will use Callback, so if you will use Delegate you will have to take two Delegates.
A callback is a function passed into another function as an argument to be executed later. A high-order function is a function that accepts another function as an argument. Callback functions can be synchronous or asynchronous.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With