Suppose if you have multiple methods with same signature (return type & number of parameters) and want to call all the methods with single object then we can go for delegates.
Delegates are two types
-Single Cast Delegates
-Multi Cast Delegates
Single Cast Delegates
Single cast delegate means which hold address of single method like as explained in above example.
Multicast Delegates
Multi cast delegate is used to hold address of multiple methods in single delegate. To hold multiple addresses with delegate we will use overloaded += operator and if you want remove addresses from delegate we need to use overloaded operator -=
Multicast delegates will work only for the methods which have return type only void. If we want to create a multicast delegate with return type we will get the return type of last method in the invocation list
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
Delegates have the following properties:
Delegates are like C++ function pointers but are type safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on a single event.
Munesh SharmaPosted Jun 18, 2015, 1:36 AM
Sujeet SumanPosted Jun 18, 2015, 1:34 AM
There are 3 steps to create a delegate:
Declaration
Instrantiation
Invocation
There are 3 types of delegate:
Singlecast delegate
Multicast delegate
Generic delegate
By the help of BeginInvoke() and EndInvoke() we can call a method asynchronously.
Upendra Pratap ShahiPosted Jun 18, 2015, 1:20 AM
Rajeesh MenothPosted Jun 18, 2015, 1:15 AM
Manoj BhoirPosted Jun 18, 2015, 12:55 AM
Delegates have the following properties:
-
-
-
-
-
For more details please check these links :Delegates are like C++ function pointers but are type safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on a single event.
Methods do not have to match the delegate type exactly. For more information, see Using Variance in Delegates (C# and Visual Basic).
Santhakumar MunuswamyPosted Jun 17, 2015, 11:39 PM