What is multicast delegate ? [Resolved]

Posted by Munder2013 under C# on 5/15/2013 | Points: 10 | Views : 9370 | Status : [Member] | Replies : 6
Hi Experts,
plz tell me
What are delegates and how to use them ?

Thanks & Regards
Baljeet Singh



Responses

Posted by: aswinialuri-19361 on: 5/15/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
hi baljeet,

When you want to create a delegate in C#, you make use of the delegate keyword. The name of your delegate can be whatever you desire.
public delegate int BinaryOp(intx, inty);
this delgate can point to any method
taking two integers and returning one integer
When the C# compiler processes delegate types, it automatically generates a sealed class deriving from System.MulticastDelegate.
we can avoid writing multiple linesof code using delegates to call the function
Multicast delegate:if a delegate is used to refer more than one function is known as multi cast delegate
I hope it will help you
mark as answer if it helps you
Thanks&Regards

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

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

Posted by: Kmandapalli on: 5/15/2013 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,

Delegates are
a .NET object which points to a method that matches its specific signature.

Delegates are function pointers that point to function of matching signatures. Function pointers which are extensively used in c/c++ to points to a function holds only the memory address of the function, it doesn't carry further information about the function parameters, return type etc. On the other hand .NET framework has introduced a type-safe mechanism called delegates, with automatic verification of the signature by the compiler.

So we can say that delegates are type-safe, object oriented, secure .NET objects which can be used to invoke methods of matching signature.

While using delegates it is very much necessary to make sure that the functions which the delegates points has the same number of argument type and same return type. For example if we have a method that takes a single string as a parameter and another method that takes two string parameters, then we need to have two separate delegate type for each method.

Delegates are of two types:

Single-cast delegates:
* If a delegate invokes a single method, then it is called as a Single cast delegate.
* Single-cast Delegates refer to a single method with matching signature.
They are derived from the System.Delegate class

Multi-cast delegates:
*They are nothing but a single delegate that can invoke multiple methods of matching signature.
*Derives from System.MulticastDelegate class which is a subclass of System.Delegate
*In this, we create a single delegate that in turn invokes multiple encapsulated methods.
We can use Multi-cast Delegates when multiple calls to different methods are required.
For example
if we are required to call two methods on a single button click event or mouse over event then using Multi-cast Delegates we can easily call the methods.

For more information, you can go through the following link,
http://dotnetslackers.com/articles/net/Delegates-in-NET.aspx

Regards,
Shree M.

Kavya Shree Mandapalli

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

Posted by: Satyapriyanayak on: 5/15/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.c-sharpcorner.com/UploadFile/ankithakur/Delegates06042007014105AM/Delegates.aspx
http://www.codeproject.com/Articles/31400/NET-Multicast-Delegates
http://msdn.microsoft.com/en-us/library/system.multicastdelegate.aspx

If this post helps you mark it as answer
Thanks

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

Posted by: Annaharris on: 5/15/2013 [Member] Starter | Points: 25

Up
0
Down
What is main purpose of using delegates in Asp.net?

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

Posted by: Kmandapalli on: 5/15/2013 [Member] Silver | Points: 25

Up
0
Down
hi,

Before we move ahead and we talk about delegates let's try to understand what problem does delegate solve. Below is a simple class named 'ClsMaths' which has a simple 'Add' function. This class 'ClsMaths' is consumed by a simple UI client. Now let's say over a period of time you add subtraction functionality to the 'ClsMaths' class, your client need to change accordingly to accommodate the new functionality.
In other words addition of new functionality in the class leads to recompiling of your UI client.

In short the problem is that there is a tight coupling of function names with the UI client. So how can we solve this problem?. Rather than referring to the actual methods in the UI / client if we can refer an abstract pointer which in turn refers to the methods then we can decouple the functions from UI.
Later any change in the class 'ClsMath' will not affect the UI as the changes will be decoupled by the Abstract pointer. This abstract pointer can be defined by using delegates. Delegates define a simple abstract pointer to the function / method.

Regards,
Shree M.





Kavya Shree Mandapalli

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

Posted by: Munder2013 on: 5/15/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks Ashwini and Shree M...


Thanks & Regards
Baljeet Singh

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

Login to post response