Created
April 22, 2017 06:38
-
-
Save caviles/fc473f82dc993c0a3bdacb8d0f72e01b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a delegate is a function pointer | |
when we work with events we use a delegate which inherie from multicast deletegate | |
a delegate is the pipeline between the event and the event handler | |
button.Click += EventHandler(btnHit_Click) // here when the click event is raised the eventhandler delegate will call the btnHit_CLick method | |
public delegate void WorkHandler(string job, int time) // this delegate can be used as the pipeline for methods that take these two args | |
public void work(string job, int time) | |
{ | |
console.write("called") | |
} | |
public void work2(string job, int time) | |
{ | |
console.write("called2") | |
} | |
var del = new WorkHandler(work); | |
del("sf",3) // this will force the pipeline to call the work method | |
var del2 = new WorkHandler(work2); | |
del += del2; | |
del("sf",3) // this will force the pipeline to call the work and work2 method | |
when the delete has a return type it will expect methods with return types | |
in a multicast scenerio where the delete is assigned to a variable the last return type will be place in that var | |
behind the scenes the delagate will be compiled into a class | |
events | |
public event WorkHandler WorkCompleted; //define an event | |
public void OnWork(){ | |
if(WorkCompleted != null) // this will make sure it has been attached | |
WorkCompleted("adsdsd", 6) // this will raise the event which will call deletegates and that will then call the work method | |
simple example | |
https://www.codeproject.com/Articles/11541/The-Simplest-C-Events-Example-Imaginable | |
another simple sample | |
https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment