C # delegated instance 10.6.25

Source: Internet
Author: User

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 );

}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.