0
Thanks Guys, I feel i have a better understanding of 'Delegates' now. I just need to put them to some use.
0
In your example there is no need to use delegates. You have no input in your application and you know every aspect of your code at design time.
In the following example the actual course of action is decided by the user.
You can also see from this example that you can think of methods as objects and you can think of delegates as classes. You can actually pass methods as parameters:
class Program { static void Main(string[] args) { string input = Console.ReadLine(); if(input == "length") { execute(length); Console.ReadKey(true); } if (input == "doublelength") { execute(doublelength); Console.ReadKey(true); } }
delegate int Method(string s);
static int length(string s) { return s.Length; }
static int doublelength(string s) { return s.Length * 2; }
static void execute(Method m) { Console.WriteLine(m("test string")); } }
|
0
Taking the following example, wouldnt it be simpler just to call the methods directly, what benifits is there to having a delegate?:
using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateEX
{
public delegate void mydele(int x, int y);
class A
{
public void add(int x, int y)
{
Console.WriteLine("The Sum is " + (x + y));
}
public void sub(int x, int y)
{
Console.WriteLine("The Difference is " + (x - y));
Console.ReadKey();
}
}
class Program
{
static void Main(string[] args)
{
A obj = new A();
//make an object of class A
mydele m = new mydele(obj.add);
m(10, 20);
mydele m1 = new mydele(obj.sub);
m1(10, 20);
}
}
}
0
You have two segments of code that you want to keep separate. The first segment decides which method (of a set) must be executed and the second segment executes the chosen method.
If all the methods in the set have the same return type and parameters:
int CharactersInCommon(string p1, string p2); int WordsInCommon(string p1, string p2); int SentencesInCommon(string p1, string p2);
|
you can create a delegate
delegate int CommonComparer(string p1, string p2);
|
and have your bits of code separate like this:
In one part of your code you decide what method you must use (based on user input):
CommonComparer Compare;
switch (input) { case 'C': Compare = new CommonComparer (CharactersInCommon); break; case 'W': Compare = new CommonComparer (WordsInCommon); break; case 'S': C ompare = new CommonComparer (SentencesInCommon); break; }
|
In a different part of your code you can use the selected method not caring what it actually is:
int count = Compare(FirstText, SecondText);
|
0
That definition means to say it knows only at runtime, not at compile time. Say I tell you that I will introduce you to someone and actually do that, but at the time of introduction, you were not aware that the person being introduced is a cousin of mine. Only later, you get to know. The same way when a delegate is created, it does not know at that stage the details of the method it is pointing to. That will be resolved only at runtime.
Consider an example. Lets say a delegate says to itself I am supposed to point to a method returning an int and accepting an int. At runtime, the ambiguity is resolved, and the delegate makes the (astonishing?!) discovery that the method it is pointing to is Calc.
Hope this makes it a little clear.
0
I have read this quote on the web 'A delegate can be used to invoke a method, the call to which can only be resolved or determined at runtime.'
How would it not know what it is at runtime.
sorry if this sounds a bit dumb, just trying to get it straight in my head.
0
From my point of view, one of the best scenarios where they come into picture is when you are writing event handlers by hand (rather than autogenerating them). Say you want to associate some user-defined method to the click event of a button. You would use a delegate in that case.
btn_Click += delegate_name;
where delegate_name points to your user-defined method that will act as the event handler.
Of course, there are plenty of other uses of delegates but this is a practical easy to understand example that I have given you.
-Mamta