Simple and easy-to-understand C # event proxy example

Source: Internet
Author: User

Event proxy often makes programmers feel a headache. After reading this example, you may be panic.

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace eventdelegateapp
{
// The deletegate function with parameters is: the old version knows who is going to play games at work. As long as an employee plays games, immediately notify the boss via proxy
Public Delegate void deletegateclasshandle (Object sender, customereventargs E );
Class Program
{
Static void main (string [] ARGs)
{
Employee Employee = new employee ();
Employee. Age = 18;
Employee. Name = "Mike ";

Admin admin = new admin ();
Employee. playgame + = new deletegateclasshandle (Admin. Policy );

Employee. Games ();
Console. Read ();

}
}
Public class customereventargs: eventargs
{
String name = "";
Int age = 0;
Public customereventargs ()
{}
Public string name
{
Get {return this. Name ;}
Set {This. Name = value ;}
}
Public int age
{
Get {return this. Age ;}
Set {This. Age = value ;}
}

}

Public class employee
{
Private string _ name;
Private int _ age;
Public string name
{
Get {return this. _ name ;}
Set {This. _ name = value ;}
}
Public int age
{
Get {return this. _ age ;}
Set {This. _ age = value ;}
}


Public event deletegateclasshandle playgame;

Public void games ()
{
If (playgame! = NULL)
{
Customereventargs EE = new customereventargs ();
Ee. Age = This. _ age;
Ee. Name = This. _ name;

Playgame (this, ee );
}
}
}
Public class Admin
{
Public void Policy (Object sender, customereventargs E)
{
Console. writeline (E. Age. tostring () + "years old" + E. Name + "is playing game ");
}
}

}

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.