What is Delegate and How many Types of Delegates in C# .Net
A Delegate was a pointer to a method which can be used for invoking a method multiple times that gets executed under stack which is reserved for it their same the concepts of function pointer in our traditional c, c++ languages.
using Delegates : To use a Delegates follow the below process
Step1 : Delegate Declaration :
Here we require the first declare a Delegate for invoking the method as following[<Modifiers>] Delegate <void/type> <Name>[<Param Def's>]
Note : The I/O Parameters of your Delegate should match with the I/O parameters of the method the Delegate was calling.
Example :
Public void Add(int x, int y) { Console.WriteLIne(x+y); }Public delegates void Adddel(int x, int y)
Example 2 :
Public string sayhello(string name) { return " Hello" + name;
}Public delegates string SayDel(string name);
Two types of Delegates :
1) Single cast Delegates 2) Multicast Delegates
If a delegate is used for invoking a single method of a class. it is referred as a singlecast delegate.
If a delegate used for invoking more than one method it's referred as a multicast Delegate.
delegates are same as pointers. they contain the address location of the method.
ReplyDeleteAnd the delegates are strongly typed.
There are two types of delegates.
Synchronous and Asynchronous Delegate........