Delegates in C#.net using wpf [Resolved]

Posted by Sudhakar_A under C# on 9/27/2013 | Points: 10 | Views : 13281 | Status : [Member] | Replies : 4
How to use delegates in C#?
pls provide some example.....




Responses

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
The beset site for delegates with wcf is as follows:-

http://msdn.microsoft.com/en-us/library/ms730088.aspx
http://msdn.microsoft.com/en-us/library/ff650896.aspx
http://msdn.microsoft.com/en-us/library/ff649252.aspx

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER


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

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Following example demonstrates declaration, instantiation and use of a delegate that can be used to reference methods that take an integer parameter and returns an integer value.

using System;

delegate int NumberChanger(int n);
namespace DelegateAppl
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}

public static int MultNum(int q)
{
num *= q;
return num;
}
public static int getNum()
{
return num;
}

static void Main(string[] args)
{
//create delegate instances
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
//calling the methods using the delegate objects
nc1(25);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:

Value of Num: 35
Value of Num: 175

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
References:
http://www.tutorialspoint.com/csharp/csharp_delegates.htm
http://www.codeproject.com/Articles/11657/Understanding-Delegates-in-C
http://geekswithblogs.net/joycsharp/archive/2008/02/15/simple-c-delegate-sample.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Multicast Delegate
It is a delegate which holds the reference of more than one method.

Multicast delegates must contain only methods that return void, else there is a run-time exception.

Simple Program using Multicast Delegate
delegate void Delegate_Multicast(int x, int y);

Class Class2
{
static void Method1(int x, int y)
{
Console.WriteLine("You r in Method 1");
}

static void Method2(int x, int y)
{
Console.WriteLine("You r in Method 2");
}

public static void <place w:st="on" />Main</place />()
{
Delegate_Multicast func = new Delegate_Multicast(Method1);
func += new Delegate_Multicast(Method2);
func(1,2); // Method1 and Method2 are called
func -= new Delegate_Multicast(Method1);
func(2,3); // Only Method2 is called
}
}


Explanation
In the above example, you can see that two methods are defined named method1 and method2 which take two integer parameters and return type as void.

In the main method, the Delegate object is created using the following statement:
Delegate_Multicast func = new Delegate_Multicast(Method1);

Then the Delegate is added using the += operator and removed using the -= operator.

Reference for knowing when & how to use delegates
http://stackoverflow.com/questions/2019402/when-why-to-use-delegates
http://stackoverflow.com/questions/31497/where-do-i-use-delegates?lq=1

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Login to post response