Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace delegatetext
{
Class textdelegate
{
// Define the delegate
Public Delegate void delegatelogin (string name, string password );
// Define the event
Public event delegatelogin eventlogin;
Private string name;
Private string password;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
Public String Password
{
Get {return password ;}
Set {Password = value ;}
}
Public textdelegate (string name, string password)
{
This. Name = Name;
This. Password = password;
}
/// <Summary>
/// I want to define this event
/// This event occurs only when I delegate the event, but the event to be subscribed must be the same as the parameter list of the delegate.
/// </Summary>
/// <Param name = "name"> </param>
/// <Param name = "password"> </param>
Public void login (string name, string password)
{
Console. writeline ("My name is {0} and the password is {1}. Only when I happen can you happen", name, password );
If (eventlogin! = NULL)
{
Eventlogin (name, password );
}
}
}
Class text
{
Static void main ()
{
// This is meaningless and serves to generate objects
Textdelegate DG = new textdelegate ("Andy Lau", "123456 ");
// Event subscription Syntax: Object Name point event name = new class name point delegate name (the parameter is the delegate method): Note that the delegate method parameter table is the same as the delegate method parameter list
DG. eventlogin + = new textdelegate. delegatelogin (userlogin );
DG. login ("Gu tianle", "123456 ");
}
Public static void userlogin (string name, string password)
{
Console. writeline ("I was commissioned, I was commissioned by {0}, and the password was {1}", name, password );
}
}
}