Mahsa Hassankashi | Sunday 1 2016 0:14
What is Delegate ?
Remember primary time when you (was newer in .net) definedyour first variable:
Delegate is like that but in the most time we assign methodinstead of variable to that.
In general declaration area write:
And inside your event assign your method to delegate:
In another words delegate is a pointer to method like C++,but with this difference that delegate is type safe due to it must be the samein number and data type for inputs and outputs: Be the same in inputs andoutputs reminds us Interface concept, so if we have interface why we needdelegate?
In fact delegate creates and raises our requirement functionin run time mode and it can be confidence representative even for more than onemethod.
Assume you want to write application which is a calculator:
In the above example if you want to invoke delegate you passan integer number to getdel method which point to the “Add”, but you can invokeall of your method sequentially (one after another one)
When you invoke delegate pointer it call all of those methodsequentially:
Actually multicast delegate is used whenever we want tobroadcast simultaneously tasks such as whenever an event is raised some relatedactions occur after that. Assume you have account in the bank and before youtold your bank to activate your notification to your “email”, “send sms to yourmobile” or “deliver print from ATM”, so after you do ATM or any transaction youwill receive all of your notification simultaneously.
Those events that determine when an event is raised arepublishers and those ones that determine what actions should be taken in theresponse of publisher event are subscriber.
And the file has been created in specific root:
Above picture shows that delegate is open in front designand if you want to block it and when write mdel (“Delegate”) and hit dot (“.”)you do not want to appear all of method such as “BeginInvoke” , “Clone” and etcso you should use eventhandler to encapsulate your delegate.
Project Name (RC) àAdd New Item àWeb User Control àName it : WebUserControl.ascx
Drag and drop one button on that
Create web form and in solution explorer click on your webuser control and drag it to your design web form such as:
Then event has upper hand to manage delegate and it is nowmore encapsulated:
|Aahna Lourn | Friday 9 2014 18:48|
|Great Article , So useful|
|Kenneth | Friday 9 2014 22:11|
|Renju | Saturday 9 2014 2:4|
|Solomon | Friday 9 2014 14:27|
|Mayur Nimbalkar | Monday 9 2014 8:26|
|Really Useful. Thank you for sharing this.|
|Sadegh | Saturday 12 2014 5:39|
|Nice ! Very Good|
|Rizwan | Wednesday 1 2015 0:54|
|very useful :)|
|Ahmed | Wednesday 1 2015 23:53|
|Very clear... Keep up the good work and good luck..|
|MAhdi | Friday 8 2015 11:53|
|great article thanks|
|Dev Sharma | Tuesday 9 2015 11:5|
|Dear Mahsa Madam, Your blog is very informative. It help me to understand topic clearly. But finding difficult to understand Delegate due to broken images. Please check this link : http://technical.cosmicverse.info/Article/Index/1|
|Mahsa Hassankashi | Sunday 1 2016 0:56|
|Dear @Dev Sharma, thank you for your accurate consideration. I have corrected images and so sorry for delay in response.|