7 November 2011

Delegates in C#

Delegates in C#


A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++.


Declaring Delegates



public delegate void Del(string message);
Once a delegate is instantiated, a method call made to the delegate will be passed by the delegate to that method.



// Create a method for a delegate.
public static void DelegateMethod(string message)
{
    System.Console.WriteLine(message);
}

// Instantiate the delegate.
Del handler = DelegateMethod;

// Call the delegate.
handler("Hello World");

Sources : http://msdn.microsoft.com/en-us/library/ms173172(v=vs.80).aspx

No comments:

Post a Comment

Comments Welcome