• A delegate in C# is used to encapsulate a method and very is similar to a function pointer in C or C++ (see how it works in C++ Link). The delegate keyword in C# when in the context to a method or variable,  encapsulates 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.

    • Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure.

  • While using delegate, you are defining a

    • type of method you will be encapsulating

    • the return type

    • For a static methods, a delegate object encapsulates the method to be called. For instance methods, a delegate object encapsulates both an instance and a method on the instance. If you have a delegate object and an appropriate set of arguments, you can invoke the delegate with the arguments.

  • delegate grammars

    • delegate return-type identifier ([parameters]);

      where:

      • keyword delegate
      • return-type:  matches the return type of the function.
      • identifier: The delegate name.
      • parameters: The Parameters, that the function takes.

       

  •  a delegate  does not know or care about the class of the object that it references.

    • Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.

 
Diagram Example
delegate ret-type name(parameter-list);
using System;
namespace testdelegate2
{
    public delegate void helloworld();
    class test
    {
        public static void method()
        {
            Console.WriteLine("hello, world!");
        }
        static void Main(string[] args)
        {
            helloworld dg = new helloworld(method);
            dg();
            Console.ReadLine();
          }
    }


}
    
Example
 

The example shown below contains a method,
  •  that will not return any value to the caller and print something locally
  • we really did not separate (encapsulate) out regiously.
using System;
using System.Collections.Generic;
using System.Text;

namespace delegate_example
{

    // #1 delcare a delegate 
    /* 
     * use the keyword delegate
     * name the method you will be encapsulating
     * and its return type 
     */
    public delegate void delegate_a_method(string str);
    // 
    class test
    {
        public static void process_request(string input)
        {
            string received = input;
            Console.WriteLine("hello, user you entered : " + received);
        }


        [STAThread]
        static void Main(string[] args)
        {
            // #2 implement a delegate with new key word just line a class
            //what is different, as if class needs a parameter
            delegate_a_method d_a_m = new delegate_a_method(process_request);
            Console.Write(" Enter something :");
            string str = Console.ReadLine();
            // #3 invoke a call through a delegate object
            d_a_m(str);

            Console.ReadLine();


        }
    }

}
 
 
 
Entering the program with F11

next, creating object and instantiating a delegate

requesting user's input

Go through F11 steps.

Delegate object is calling the method

At this point if point string input with your mouse you will note