Delegate with real time example

Posted by Rajasekhar0544 under C# on 10/25/2012 | Points: 10 | Views : 30330 | Status : [Member] | Replies : 6
Hi,

I am unable to understand what is the difference between a normal method and a delegate method. Can you please explain with a real time example and write its advantages and disadvantages ?????????????




Responses

Posted by: Naraayanan on: 10/25/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Check this link
http://www.codeproject.com/Questions/357745/Delefates-and-Normal-Methods

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Seng2hs on: 10/25/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Below link will give a detailed idea about delegate

http://www.codeproject.com/Articles/71154/C-Delegates-101-A-Practical-Example

Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gow.Net on: 10/25/2012 [Member] Starter | Points: 25

Up
0
Down
 public class sample

{
public void methode1()
{
Console.WriteLine("You r in method1");

}
public void methode2()
{
Console.WriteLine("You r in method2");

}
public void methode3()
{
Console.WriteLine("You r in method3");

}
}

class Program
{

static void Main(string[] args)
{
sample s = new sample();
Console.WriteLine("Single Delegate");
Delegateexample obj_dgt = new Delegateexample(s.methode1);

Console.WriteLine("Multicast Delegate");
obj_dgt += new Delegateexample(s.methode2);
obj_dgt += new Delegateexample(s.methode3);
obj_dgt.Invoke();

Console.WriteLine("Delegate");
obj_dgt -= new Delegateexample(s.methode3);
obj_dgt.Invoke();

}
}


gowthaman8870226416

Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 10/25/2012 [Member] Starter | Points: 25

Up
0
Down
hi..

for simplest way to understand Delegate Concept...

check this link:

http://www.dotnetfunda.com/blogs/sourabh07/3431/delegate-concept-with-simplest-example-interview-point-of-view

Sourabh07

Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dharanidhar on: 10/30/2012 [Member] Starter | Points: 25

Up
0
Down
hi...
Delegate is different from normal method.Delegate is a type which holds method reference in an object.It is also called as type safe function pointer.It is similar to C++ pointer.But the difference is c++ pointers are not type safe.It can point to any type of method.but delegate is type safe.we cannot point int type delegate to a string type.
A delegate should have the same signature and same number of parameters as that of a method to encapsulate the method or to create a reference for that method.
Example:-
public delegate int Delegate_Sum(int a,int b); //This is a delegate which refers to the method "Sum".Observe that the delagate has same signature and same number of parameters as that of method.

class Class1
{
static int Sum(int val1,int val2)
{
return val1*val2;
}
static void Main(string[] args)
{
Delegate_Sum delObj = new Delegate_Sum(Sum); //Creating a delegate instance and passing the parameter as Method Name
Console.Write("Please Enter Values");
int v1 = Int32.Parse(Console.ReadLine());
int v2 = Int32.Parse(Console.ReadLine());
//use a delegate for processing
int res = delObj(v1,v2); //After this, we are accepting the two values from the user and passing those values to the delegate as we do using method.Here delegate object encapsulates the method functionalities and returns the result.
Console.WriteLine ("Result :"+res);
Console.ReadLine();
}
}

Some of the advantages of Delagates are:-
1.It encapsulates the method call
2.It increases the performance of the application
3.We can call a method Asynchronously using a delegate.We have multicast delegates as well...

Thank you




Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jointovijay on: 8/9/2013 [Member] Starter | Points: 25

Up
0
Down
Hi
Check the below link you will get some good idea

http://asp-net-corner.blogspot.in/2013/07/simple-delegate-example.html

http://www.codeproject.com/Articles/71154/C-Delegates-101-A-Practical-Example




Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response