A Delegate is a type variable that holds the reference to a method. Delegates are similar to Pointer to functions in C and C++
When we create a delegate then we can assign a method to delegate instance according to its signature and we can change the reference at run time. After assigning the method we can call this method using delegate instance.
Note
- Your method signature and return type must match with Delegate
Why do we need to create a Delegate ?
- Delegate Invoke method at run time.
- Delegate can be used for callback methods.
- Delegates can invoke multiple methods on a single event. So this is useful when we have to give multiple implementation of a single method.
- When do you want to restrict access to caller then we can expose a delegate.
Now How can we create a Delegate ?
public delegate int MyPointer(int x, int y);
AccessModifier, delegate type , return type DelegateName(Delegate params)
This is how we can create a Delegate. Now the point is how can we create a Delegate instance and add methods at run time.
So let's check out this example -
public class MyDelegate { public delegate int MyPointer(int x, int y); /// <summary> /// CTOR /// </summary> public MyDelegate() { } /// <summary> /// /// </summary> /// <param name="operation"></param> /// <returns></returns> public MyPointer MyOperation(int operation) { MyPointer objPointer = null; if( operation == 1) { objPointer = Add; } else if (operation == 2) { objPointer = Sub; } else if (operation == 3) { objPointer = Mul; } else if (operation == 4) { objPointer = Div; } return objPointer; } private int Add(int x, int y) { return x + y; } private int Sub(int x, int y) { return x - y; } private int Mul(int x, int y) { return x * y; } private int Div(int x, int y) { return x / y; } }
How MainMethod will call delegate and execute the methods - Let's have a look
static MyDelegate objDel; static void Main(string[] args) { objDel = new MyDelegate(); Delgateinvoke(); }
private static void Delgateinvoke() { //Way 1 int num = objDel.MyOperation(1).Invoke(4, 5); //Way2 MyDelegate.MyPointer delegateInstance = objDel.MyOperation(1); delegateInstance.Invoke(4, 5); Console.WriteLine(num.ToString()); }
So here we created instance of a class MyDelegate and
call the method MyOperation.It returns a delegate instance now we call the
invoke method of delegate.
Looks Cool..
Now the question comes What is this Invoke ? See below
example -
So delegate has these mentioned methods for instantiation. Currently I am using Invoke method to instantiate the delegate. When we say instantiate it
means delegate call the same method which we attach.
Let's talk about these methods
mentioned above -
BeginInvoke
- Executes asynchronously on a pooled thread (Thread pooling or TPL)
Invoke
- Delegate execute synchronously on the same thread.
DynamicInvoke-
Delegate execute synchronously but as name says dynamic, you pass the parameter
to function does boxing and unboxing at runtime. It requires an object array to
execute the method. Avoid this as it is Damn Slow.
GetInvocationList
- This Fetches all the method
info you associated with the Delegate. So it is useful when you want to execute
delegate according to your need. You can fetch the info and invoke accordingly.
We will discuss this in next articles.
Types
Of Delegates :
- Single Delegate
- Multi Cast Delegate
- Readymade Delegates - Func, Action and Predicate
In this article we talked about single delegate . In Next
Article we will talk about Multi Cast Delegate and Readymade Delegates in C#
Hope you enjoyed reading the article. Please inbox me in
case of any confusion.
Happy Coding :)
Great post. Let more posts and information flow in more frequently. :)
ReplyDeleteA Beginner's Guide to Casino Games - YouTube
ReplyDeleteIf you want to learn how to play casino games for money online, and youtube mp3 start to enjoy online gambling, then you've come to the right place.
Bursa
ReplyDeleteMersin
izmir
Rize
Antep
PDBL
kars
ReplyDeletesinop
sakarya
ankara
çorum
OWO
yozgat
ReplyDeletesivas
bayburt
van
uşak
F3TKDC
Bolu Lojistik
ReplyDeleteMardin Lojistik
Kocaeli Lojistik
Diyarbakır Lojistik
İstanbul Lojistik
N5S
istanbul evden eve nakliyat
ReplyDeletebalıkesir evden eve nakliyat
şırnak evden eve nakliyat
kocaeli evden eve nakliyat
bayburt evden eve nakliyat
F0YRDK
sivas evden eve nakliyat
ReplyDeleteerzurum evden eve nakliyat
bitlis evden eve nakliyat
mardin evden eve nakliyat
rize evden eve nakliyat
İDB63İ
71E81
ReplyDeleteKarapürçek Parke Ustası
Zonguldak Evden Eve Nakliyat
Yenimahalle Boya Ustası
Edirne Lojistik
Erzincan Lojistik
Mamak Boya Ustası
Sivas Parça Eşya Taşıma
Kocaeli Şehir İçi Nakliyat
Mersin Evden Eve Nakliyat
59313
ReplyDeletePoloniex Güvenilir mi
Ünye Marangoz
Bursa Şehir İçi Nakliyat
Kars Şehir İçi Nakliyat
Erzurum Lojistik
Burdur Evden Eve Nakliyat
Batıkent Parke Ustası
Sakarya Lojistik
Van Parça Eşya Taşıma
DEE71
ReplyDeleteÇerkezköy Mutfak Dolabı
Kırşehir Şehir İçi Nakliyat
Antalya Lojistik
Silivri Fayans Ustası
Sakarya Evden Eve Nakliyat
Edirne Lojistik
Mardin Parça Eşya Taşıma
Amasya Evden Eve Nakliyat
Konya Lojistik
BB23B
ReplyDeleteorder testosterone propionat
order testosterone enanthate
Elazığ Evden Eve Nakliyat
Silivri Boya Ustası
Kütahya Evden Eve Nakliyat
buy testosterone enanthate
Yozgat Evden Eve Nakliyat
order sarms
Çerkezköy Evden Eve Nakliyat
AD53E
ReplyDeleteJns Coin Hangi Borsada
Bitcoin Nasıl Kazılır
Parasız Görüntülü Sohbet
Alya Coin Hangi Borsada
Bitcoin Kazma Siteleri
Coin Üretme
Referans Kimliği Nedir
Bitcoin Nasıl Üretilir
Twitter Beğeni Satın Al
AC45B
ReplyDeleteBitcoin Mining Nasıl Yapılır
Binance Referans Kodu
Vector Coin Hangi Borsada
Kaspa Coin Hangi Borsada
Big Wolf Coin Hangi Borsada
Referans Kimliği Nedir
Azero Coin Hangi Borsada
Mexc Borsası Güvenilir mi
Kwai Takipçi Hilesi