June 26, 2007
Hi Guys
Callback Methods through Delegates in C# is explained in the following website but it seems heavy example for the C# beginners. Does anyone know any other websites which provide simple explanation about callback Methods. Please help me
http://www.aspfree.com/c/a/C-Sharp/C-Sharp-Delegates-Explained/4/
Thank you
Loading
Posted Jun 29, 2007, 5:31 AM
Thank you Jan Montano
Jan MontanoPosted Jun 28, 2007, 11:15 PM
Here's a detailed description of callback, worth reading. :)
http://en.wikipedia.org/wiki/Callback_(computer_science)
Posted Jun 28, 2007, 9:10 AM
Thank you very much for your explanation Alan
Thank you very much for your example Jibasingh. I understood it
AlanPosted Jun 27, 2007, 12:10 PM
First of all I hadn't noticed jebasingh's excellent example of delegate callback which I think he must have posted as I was typing mine in, as he beat me by 16 seconds ;)
To answer your question, you only need to use += when adding a new delegate to the invocation list of a pre-existing delegate. As I wasn't using multicast delegates in my example, I didn't need to use it.
When used like this a delegate is called a callback, because the method which accepts it as a parameter, uses the delegate to 'call back' the method it points to and pass it some information and/or to obtain some information from that method.
Posted Jun 27, 2007, 10:01 AM
June 27, 2007
Thank you Jibasingh. I am trying to understand your example.
Thank you Alan for providing simple example. += sign is used when assigning more delegate objects. Here you didn’t use it. I don’t know why. Perhaps all implementations are in the same class. Please explain if my reason is not correct. Anyhow program is working well. I can use your example as a launch pad to understand more complicated examples.
Is there any reason why it is called “callback”.
cbd = new CallbackDelegate(Cube);
AlanPosted Jun 27, 2007, 6:41 AM
TBH, I couldn't find a more understandable example than this because the 'real world' use of callbacks is not really a beginner's topic.
However, this very simple console application may help you to understand the mechanism. The point here is that the Power() method neither knows nor cares whether it is supposed to square or cube a given number. Instead it is passed a delegate which points to the method which will do the appropriate work. The result is then returned to Power() which returns it to the calling code.
using System;
//define delegate class
delegate int CallbackDelegate(int result);
class Callback
{
static void Main()
{
int number = 2;
CallbackDelegate cbd = new CallbackDelegate(Square);
Console.WriteLine("{0} squared is {1}", number, Power(number, cbd));
cbd = new CallbackDelegate(Cube);
number++;
Console.WriteLine("{0} cubed is {1}", number, Power(number, cbd));
Console.ReadLine();
}
static int Power (int number, CallbackDelegate cbd)
{
return cbd(number); // call the 'callback' method and return the result
}
static int Square(int number)
{
return number * number;
}
static int Cube(int number)
{
return number * number * number;
}
}
jebasinghPosted Jun 27, 2007, 6:41 AM
hope , you can understand the concept of delegate callback from here... best of luck
using System;
// declare a delegate type
// delegate references a method that accept a parameter of type string
public delegate void del(string message);
// class sample delegate
class delegateSample
{
//static method reference by the delegate
public static void DelegateMethod(string message)
{
Console.WriteLine(message);
}
// here the delegate callback is actually happen
public void process(del callback)
{
for(int i=1; i< 10;i++)
{
callback("C# corner"+ i);
}
}
}
// Main class , since i created this as console application....
class mainclass
{
static void Main()
{
delegateSample delSam = new delegateSample();
del handler = new del(delegateSample.DelegateMethod);
delSam.process(handler);
}
}