|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionA Delegate is a class. We create an object from this class. This object can be used to store a reference to a method. Once a reference to a method is stored in the delegate object, we can invoke the method using the delegate. Let's start to work with the Delegates. Let's go step by step. We will start with an empty class. P1.csclass Del
{
}
Ok! Let's not compile yet. Now let's
add a method to the above class. Let the name of the method be This method will have access specifier as public. The program looks like this. P1.csclass Del
{
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
Now let's declare a delegate which will be
responsible for storing the reference to the function P1.csclass Del
{
delegate void dl1();
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
Now let's define a function P1.csclass Del
{
delegate void dl1();
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
public void fn2()
{
}
}
Ok! Now let's add code to the above function to
store the reference to the method P1.csclass Del
{
delegate void dl1();
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
public void fn2()
{
dl1 obj = new dl1(fn1);
}
}
Ok! Now let us invoke P1.csclass Del
{
delegate void dl1();
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
public void fn2()
{
dl1 obj = new dl1(fn1);
obj();
}
}
Cool! Now let's add another class example and
have the method P1.csclass Del
{
delegate void dl1();
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
public void fn2()
{
dl1 obj = new dl1(fn1);
obj();
}
}
class Example
{
public static void Main()
{
}
}
Now let us create an object of class P1.csclass Del
{
delegate void dl1();
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
public void fn2()
{
dl1 obj = new dl1(fn1);
obj();
}
}
class Example
{
public static void Main()
{
Del d = new DemoDeligates();
d.fn2();
}
}
OutPutInside
f1
Ah! At last we got the desired output. Aren't delegates easy? Let's go though one more program if you found it hard to digest. Consider the following program. P2.csclass Demo
{
public static void Main()
{
}
}
In the above program we have a simple class
P2.csclass Demo
{
public static void Main()
{
}
public static void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
We have added a function named P2.csclass Demo
{
delegate void d();
public static void Main()
{
}
public static void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
We have declared a delegate Now let's create an object P2.csclass Demo
{
delegate void d();
public static void Main()
{
d obj = new d(fn1);
}
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
Now let us invoke P2.csclass Demo
{
delegate void d();
public static void Main()
{
d obj = new d(fn1);
obj();
}
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
OutputInside
fn1
The above program compiles and runs
successfully to give the desired output. Wasn't that simple? But did you not
wonder how are we allowed to use P3.csclass Demo
{
delegate void d();
public static void Main()
{
d obj = new d(fn1);
obj();
obj.Invoke();
}
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
OutputInside
fn1
Inside
fn1
The above program compiles and runs successfully to give the desired output. Ok! Now consider the following program. P3.csclass Demo
{
delegate void d();
public static void Main()
{
d obj = new d(fn1);
obj();
}
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
OutputInside
fn1
The above program compiles and runs
successfully to give the desired output. The delegate object P4.csclass Demo
{
delegate void d();
public static void Main()
{
d obj = new d(fn1);
obj();
}
public void fn1()
{
System.Console.WriteLine("Inside f1");
}
public void fn2()
{
System.Console.WriteLine("Inside f2");
}
}
OutputInside
fn1
The
above program compiles and runs successfully to give the desired output. Now
what do we do to invoke P5.csclass Demo
{
delegate void d();
public static void Main()
{
d obj = new d(fn1);
obj();
obj += new d(fn2);
obj();
}
public static void fn1()
{
System.Console.WriteLine("Inside f1");
}
public static void fn2()
{
System.Console.WriteLine("Inside f2");
}
}
OutputInside
fn1
Inside
fn1
Inside
fn2
The
above program compiles and runs successfully to give the desired output. The
statement that does the magic of adding method references to delegates is Take the following program. P6.csclass Demo
{
delegate void d(int x);
public static void Main()
{
d obj = new d(fn1);
obj();
}
public static void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
Outputa.cs(7,14):
error CS0123: No overload for 'fn1' matches delegate 'Demo.d'
a.cs(3,17):
(Location of symbol related to previous error)
a.cs(11,22):
(Location of symbol related to previous error)
a.cs(8,6):
error CS1593: Delegate 'd' does not take '0' arguments
a.cs(3,17):
(Location of symbol related to previous error)
Bummer! We got an error! That's because we
declared the delegate P7.csclass Demo
{
delegate void d();
public static void Main()
{
d obj = new d(fn1);
obj();
}
public static void fn1()
{
System.Console.WriteLine("Inside f1");
}
}
OutputInside
f1
Passing Delegates to functionsCan you pass delegates to functions? Yes. Why not? Let's see how we do this. P8.csclass Demo
{
delegate void del();
public static void Main()
{
del obj = new del(fn1);
foo(obj);
}
public static void fn1()
{
System.Console.WriteLine("Inside f1");
}
static void foo (del d)
{
d();
}
}
OutputInside
f1
The above program compiles and runs
successfully to give the desired output. The delegate object
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||