Delegate Beginner stage
-
Can some one help me out of understanding the delegate. we are calling some method with delegate object. program t1=new program() delegateabc d1 = new delegateabc(t1.Testabc); Here I am calling the method Testabc from class program. I don't understand what is the benefit of calling through delagate I can directly use t1.testabc not going through delagte. can someone help me ou with the difference. t
-
Can some one help me out of understanding the delegate. we are calling some method with delegate object. program t1=new program() delegateabc d1 = new delegateabc(t1.Testabc); Here I am calling the method Testabc from class program. I don't understand what is the benefit of calling through delagate I can directly use t1.testabc not going through delagte. can someone help me ou with the difference. t
The code wouldn't call the function. For this you would have to add:
d1(probablySomeArguments);
In this simple scenario it doesn't make any sense. Let me try to construct a rather simple case where it could be useful: Lets assume you have an array of numbers which you want to apply a function to each one:
public void Calculate(int[] numbers) {
for (int i = 0; i < numbers; i++)
numbers[i] = numbers[i] * 2;
}This small snippet would double all numbers. But what if several callers want to do different things with those numbers? For this you *could* use a delegate:
public delegate int NumberCalculaterCallback(int number);
public void Calculate(int[] numbers, NumberCalculaterCallback callback) {
for (int i = 0; i < numbers; i++)
numbers[i] = callback(numbers[i]);
}Now the caller could input any function it likes:
public class Caller {
private int Double(int number) {
return number * 2;
}private int AddOne(int number) {
return number + 1;
}private int Square(int number) {
return Math.Pow(number, 2);
}public void Test() {
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
Calculate(numbers, new NumberCalculaterCallback(Double));
Calculate(numbers, new NumberCalculaterCallback(AddOne));
Calculate(numbers, new NumberCalculaterCallback(Square));
}
}Calculate now uses functions which it has absolute no clue about. This function could be anywhere, even in a different assembly. It just needs to know about the delegate definition and can then make any processing with the numbers the caller can think of.
-
The code wouldn't call the function. For this you would have to add:
d1(probablySomeArguments);
In this simple scenario it doesn't make any sense. Let me try to construct a rather simple case where it could be useful: Lets assume you have an array of numbers which you want to apply a function to each one:
public void Calculate(int[] numbers) {
for (int i = 0; i < numbers; i++)
numbers[i] = numbers[i] * 2;
}This small snippet would double all numbers. But what if several callers want to do different things with those numbers? For this you *could* use a delegate:
public delegate int NumberCalculaterCallback(int number);
public void Calculate(int[] numbers, NumberCalculaterCallback callback) {
for (int i = 0; i < numbers; i++)
numbers[i] = callback(numbers[i]);
}Now the caller could input any function it likes:
public class Caller {
private int Double(int number) {
return number * 2;
}private int AddOne(int number) {
return number + 1;
}private int Square(int number) {
return Math.Pow(number, 2);
}public void Test() {
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
Calculate(numbers, new NumberCalculaterCallback(Double));
Calculate(numbers, new NumberCalculaterCallback(AddOne));
Calculate(numbers, new NumberCalculaterCallback(Square));
}
}Calculate now uses functions which it has absolute no clue about. This function could be anywhere, even in a different assembly. It just needs to know about the delegate definition and can then make any processing with the numbers the caller can think of.