Im trying to get my head around using Delegates. Can someone give me a scenario where i would use these.
Thanks
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mamta MRank #268 · 9.4K reputationPosted Aug 3, 2010, 2:03 AM EDT
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
Dave17 reputationPosted Aug 3, 2010, 2:39 AM EDT
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.
Mamta MRank #268 · 9.4K reputationPosted Aug 3, 2010, 2:47 AM EDT
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.
Alexandru Lazar12 reputationPosted Aug 3, 2010, 2:57 AM EDT
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
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': Compare = new CommonComparer (SentencesInCommon); break; }Dave17 reputationPosted Aug 3, 2010, 3:41 AM EDT
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);
}
}
}
Alexandru Lazar12 reputationPosted Aug 3, 2010, 6:00 AM EDT
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")); } }Dave17 reputationPosted Aug 3, 2010, 7:32 AM EDT