Delegate and event 10.6.23

Source: Internet
Author: User

1. C # One of the important features of support is the support for delegate and event. The delegation and event are fully compatible. The delegate can be understood as a pointer to the function, that is to say, he can reference methods (functions) and do so through the address transfer mechanism.

Avoid: The delegate is equivalent to a pointer. You can reference the method and use the address transfer mechanism to complete it.

2. With the help of delegation, events use delegation to call methods in objects of subscribed events

Avoid: events use delegation to call subscribed events.

3. Define the delegate Public Delegate void login (string name); no method body

4. C # An event allows an event of an object to notify other objects. The notification object is called the event issuer. The publisher can subscribe to an event, which is also called a subscriber. An event can have multiple subscribers, the notified object can subscribe to events, or subscriber.

For example:

For example, in the track and field race, the athletes are ready to concentrate on watching the referee's whistle. Prepare the referee and blow the whistle. All athletes start to race. In this way, we can see that the referee is the event owner, also called the event issuer. Athletes should be an event, so they are also called event subscribers. Our audience did not race, and all did not subscribe to the event.

5. to define an event, first define the delegate, and then define the event based on the defined delegate (to see if you want to define the event, as the audience does not have to race, so we do not define the event)

Syntax:

Public Delegate void delegatelogin ();

Public event login eventlogin; //////////////////////////////////////// // events are scheduled to have no parentheses

There is no method body for the event. The delegate time limit determines the type of the event to be triggered, as well as the number of parameters, types, and return values of the function.

If you want to delegate permissions to me, you must follow my requirements to understand a little different from reality: Pay attention

6. subscribe to events

You only need to add a delegate. When an event is triggered, the delegate starts to call a method selectively. Scheduled event

Eventlogin + = new delegatelogin (obj. method );

Event name (Method Name of the scheduled event) + =

Triggered after the event is scheduled

If (condition)

{

// Events

Eventlogin ();

}

########### Delegate an event

I. Example of delegation below

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

Namespace lessionrraylist
{
/// <Summary>
/// Define the delegate
/// No method implementation, no method body
/// </Summary>
Public Delegate void login (string name, string password );
Class presion
{
/// <Summary>
/// The delegate is equivalent to a pointer and is used to reference the method. It is completed through the address transfer mechanism.
/// The following is the reference method
/// </Summary>
Public void userlogin (string name, string password)
{
Console. writeline ("User Logon {0}/t {1}", name, password );
}
Public void adminlogin (string name, string password)
{
Console. writeline ("Admin Logon {0}/t {1}", name, password );
}

}
Class deletegateprogram
{
Static void main1 (string [] ARGs)
{
Presion = new presion ();
// Instantiate the delegate (pass a method parameter in): Note that the parameters of the method are not in brackets.
Login Login = New Login (presion. userlogin );
// Assign a value to the passed method parameter
Login ("Xiaogang", "123456 ");
Console. Readline ();
}
}
}
Ii. Example of delegation in the following event

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

Namespace lessionrraylist
{

Class users
{
// Delegated Logon
Public Delegate void delegatelogin ();
// Defines that event logon must conform to the delegate type. No method brackets exist.
Public event delegatelogin eventlogin;
// Class field
Private string name;
// Class attributes
Public string name
{
Get {return name ;}
Set {name = value ;}
}
Private string password;
Public String Password
{
Get {return password ;}
Set {Password = value ;}
}

// Events that cause logon and subscribe to events
Public void login ()
{
Console. writeline ("logging on ");
If (eventlogin! = NULL)
{
Eventlogin ();
}
}
}
Class eventprogram
{
Static void main ()
{
Users user = new users ();
User. Name = "aogang ";
User. Password = "123456 ";
// Pay attention to the order. After an event is triggered, it is meaningless to subscribe to the delegate. The delegate is subscribed before the event occurs.
// Define the event: the event is based on the successful login delegate, but the event must be triggered first.
User. eventlogin + = new users. delegatelogin (user_login );
// Event triggered, causing Logon
User. login ();

Console. Readline ();

}
Static void user_login ()
{
Console. writeline ("Logon successful !!!! ");
}
}
}

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.