Quote:
Originally Posted by chinaNoob
hi there,
what's difference between delegate and common function?
|
Really there is no difference, the delagate is a type that encapsulate a method, simalar to a function pointer in C if you are familar with those. it allows you pass functions around as types which is the basis of event handler which are delagates.
example
Code:
// delegate declaration
public delegate void DelegateHandler(string message);
// some method definition
public void MyHelloFunction(String message)
{
Console.WriteLine("Hello from delagate");
}
// assing MyHelloFunction to the delegate; we can do this because the MyHelloFunction signiture matches the DelegateHandler signiture
DelegateHandler myHandler = MyHelloFunction;
// call the hello function via the delegate
myHandler();