Posts

C# Delegates simple example in .NET

Delegates A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. A delegate will allow us to specify what the function we'll be calling  looks like  without having to specify  which  function to call . The declaration for a delegate looks just like the declaration for a function, except that in this case, we're declaring the signature of functions that this delegate can reference. There are three steps in defining and using delegates: Declaration Instantiation Invocation A very basic example ( SimpleDelegate1.cs ): using System; namespace Akadia.BasicDelegate {      // Declaration      public delegate void  SimpleDelegate ();  ...