| Author: praveen 20 Sep 2008 | Member Level: Gold | Rating:  Points: 6 |
Hi
Delegates in C# .Net: If we look at C++ there is a feature called callback function. This feature uses Pointers to Functions to pass them as parameters to other functions. Delegate is a similar feature but it is more type safe, which stands as a stark contrast with C++ function pointers. A delegate can hold reference/s to one more more functions and invoke them as and when needed.
A delegate needs the method's name and its parameters (input and output variables) when we create a delegate. But delegate is not a standalone construction. it's a class. Any delegate is inherited from base delegate class of .NET class library when it is declared. This can be from either of the two classes from System.Delegate or System.MulticastDelegate.
If the delegate contains a return type of void, then it is automatically aliased to the type of System.MulticastDelegate. This can support multiple functions with a += operator. If the delegate contains a non-void return type then it is aliased to System.Delegate class and it cannot support multiple methods.
Let us have a look at the following sample code.
class Figure { public Figure(float a, float b, float c) { m_xPos = a; m_yPos = b; m_zPos = c; } public void InvertX() { m_xPos = - m_xPos; }
public void InvertY() { m_yPos = - m_yPos; }
public void InvertZ() { m_zPos = - m_zPos; }
private float m_xPos = 0; private float m_yPos = 0; private float m_zPos = 0;
}
Now, we have a class named Figure and it has three private fields that use to store position and three methods to invert this position by every axis. In main class we declare delegate as follows:
public delegate void FigureDelegate();
And now in the main function we should use it like this: Figure figure = new Figure(10,20,30); FigureDelegate fx = new FigureDelegate(figure.InvertX); FigureDelegate fy = new FigureDelegate(figure.InvertY); FigureDelegate fz = new FigureDelegate(figure.InvertZ); MulticastDelegate f_del = fx+fy+fz;
In this example we create three delegates of FigureDelegate type and attach to these elements our three methods from Figure class. Now every delegate keeps the address of the attached function. The last line of code is very interesting, here we create a delegate of base type (MulticastDelegate) and attach three of our already created delegates. As all our methods are of void return type they are automatically of type MutlticastDelegate and a MulticastDelegate can support multiple methods invocation also. Hence we can write
Figure figure = new Figure(10,20,30); FigureDelegate fMulti = new FigureDelegate(figure.InvertX); fMulti += new FigureDelegate(figure.InvertY); fMulti();
regards praveen
|
| Author: Shanthi M 20 Sep 2008 | Member Level: Gold | Rating:  Points: 6 |
Implementation of delegate
using System; //delegate declaration delegate int ArithOp(int x,int y); class MathOperation { // delegates method definition public static int Add(int a, int b) { return (a+b); } public static int Sub(int a,int b) { return (a-b); } } class DelegateTest { public static void Main() { //delegate instances ArithOp operation1=new ArithOp(MathOperation.Add); ArithOp operation2=new ArithOp(MathOperation.Sub); // invoking delegates int result1=operation1(200,100); int result2=operation2(200,100); Console.WriteLine("Result1="+result1); Console.WriteLine("Result2="+result2); } }
|
| Author: Bunty 20 Sep 2008 | Member Level: Diamond | Rating:  Points: 6 |
Hi,
Delegates are used for holding one or more function address.Delegates are the unsafe code from the .net framework.Delegates are reference type.There are two types of delegates in .net
1>Single cast delegate:
Example:
This code shows how to use single cast delegate in C#.Net
public delegate void dname(); class DelegateDemo { public void m1() { MessageBox.Show("From m1"); } }
private void button1_Click(object sender, EventArgs e) { DelegateDemo obj = new DelegateDemo(); dname d1 = new dname(obj.m1); d1(); } 2>Multi cast delegate:
Example:
This code demonstrate how to use multicast delegate in C#.Net
public delegate void dname(); class DelegateDemo { public void m1() { MessageBox.Show("From m1"); } public void m2() { MessageBox.Show("From m2"); } }
private void button1_Click(object sender, EventArgs e) { DelegateDemo obj = new DelegateDemo(); dname d1 = new dname(obj.m1); dname d2 = new dname(obj.m2); dname d3; d3 = d1 + d2; d3(); }
Regards S.S.Bajoria
Thanks & Regards S.S.Bajoria
|
| Author: Vidhya 20 Sep 2008 | Member Level: Gold | Rating:  Points: 6 |
hi, Delegates are fun. If you've been programming for any length of time, you've been introduced to the concept of delegates as function pointers. As a quick review, pointers are used to store the address of a thing. By changing the address contained in a pointer, the same pointer can reference multiple things during the course of execution of a program. Thus, a pointer named "stackTop" can point to an infinite number of things as they are pushed and popped from a stack implementation. We are most familiar with pointers pointing to pieces of information. However, since pointers just store addresses under the covers, they can point to other program data as well. In particular a pointer can store the location of a function entry point and a programmer can use the pointer to instruct a computer to execute at the location stored in the pointer. Typically, the role of telling the computer how to hop around is relegated to the compiler and all the jumps are determined before the program starts running. However, it is sometimes not possible to know all the jumps when a program is compiled and the jumps need to be determined by a program as it executes. This is where we typically use function pointers.
Delegates behave similarly to function pointers in C and C++. In C a programmer can create a type-safe pointer to a function and store those pointers just like any other pointer. These pointers can be assigned and later used to call the functions they reference. In C++ and other object languages, things get a bit more tricky. In object-oriented languages, most function processing is done in the context of an object instance (method call). Consider the case where a class implements a generic print function that takes no arguments and returns void. Let's consider further that calling the print function on an object instance emits the object's state information to the console. Now let's consider that we create a pointer to the print function where the pointer's definition is "a function that takes no arguments and returns void"
Regards, Vidhya
|