Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace ConsoleApplication7
{
public delegate void EH (string strText); //First step: Define a new delegate type (class)
Class Eventsourse
{
public event EH Textout;//Declare new delegate variable (instance variable)
public void Triggerevent ()
{
if (null! = Textout)
Textout ("Event triggered");//Third step: Call Delegate
}
}
Class TESTAPP
{
static void Main (string[] args)
{
Eventsourse evsrc = new Eventsourse (); Instantiate the EventSource class
Evsrc. Textout + = new EH (catchevent); ///Step two: Instantiate Evsrc. Textout----Delegate Catchevent static method to Ecsrc.textout (instance variable)
Evsrc. Triggerevent (); ////Validation Delegate//call method catch on delegate's behalf
Evsrc. Textout-= new EH (catchevent);
Evsrc. Triggerevent ();
TESTAPP Theapp = new TestApp ();
Evsrc. Textout + = new EH (theapp.instancecatch);
Evsrc. Triggerevent ();
Console.read ();
}
public static void Catchevent (String strText)
{
Console.WriteLine (StrText);//Through TextOut (StrText), where strtext= "event triggered" call, catchevent, so output Event triggered
}
public void Instancecatch (string strText)
{
Console.WriteLine ("instance" + StrText);
}
}
}
The study of C # learning-----re-commissioned