using System;
using System.Collections.Generic;
using System.Text;
namespace delegate_encapsulate
{
// #1 declare a delegate
/*
* use the keyword delegate
* name the method you will be encapsulating
* and its return type
*/
// start a new class with curlies
class mydelegales
{
public delegate void delegate_a_method(string str);
// delegates first or constructor first
// for the class constructor will be the first one to evoked
public mydelegales() { Console.WriteLine("Constructor evoked"); }
~mydelegales() { Console.WriteLine("Destructor evoked"); }
//create a method with delegate
public static void process_request(string input)
{
string received = input;
Console.WriteLine("hello, user you entered : " + received);
}
}
class test
{
[STAThread]
static void Main(string[] args)
{
// #2 implement a delegate with new key word just line a class
//what is different?
//a class holds a delegate/pointer to a method encapsulated
//stay idle till called by a method in that external class.
//since it is static method, you need to call like this
//(mydelegales.process_request);
mydelegales.delegate_a_method d_a_m = new mydelegales.delegate_a_method(mydelegales.process_request);
Console.Write(" Enter something :");
string str = Console.ReadLine();
// #3 invoke a call through a delegate object
d_a_m(str);
Console.ReadLine();
}
}
}

step 1:

Entering the Main()

Step 2 accessing the method

Step 3: Enter somethiing

Step 4 : entered my name

Step 5:

delegate is rolling

Step 6: delegates checks the method that will print out what ever you
enter at the prompt. Note input holds the name I entered earlier

Step 7: Intially string received was null and then acquires the name
from another variable of same type that is "input"

Step 9

step 10: Coming back to delegate

step 12: Job done ready to quit

No instances and no need of cleaning the abject; although delegates are
in a class, it works on its own space and did not evoke constructor (since
there is no instances/object created, constructor was not summoned. To
review an example of using delegate as a function that will be traverse
through an object, please follow the link.Link