General understanding C #(5 events)

Source: Internet
Author: User

5. Events

C # provides direct support for events. Although event processing has always been a basic part of programming, it is surprising that most languages do little effort on the concept of normalization. If you look at how the current mainstream framework handles events, we can give the following example: Delphi's function pointer (called closure) and Java's internal class adapter, and of course Windows API message system. C # uses the delegate and event keywords to provide a refreshing event processing solution. I think the best way to describe this mechanism is to give an example to illustrate the process of declaring, triggering, and processing events:

// The delegate Declaration defines the method signature that can be called "]

Public delegate void ScoreChangeEventHandler (int newScore, ref bool cancel );

// Event generation class

Public class Game

{

File: // noteIntended keywords

Public event ScoreChangeEventHandler ScoreChange;

Int score;

// Attribute Score

Public int Score

{

Get

{

Return score;

}

Set

{

If (score! = Value)

{

Bool cancel = false;

ScoreChange (value, ref cancel );

If (! Cancel)

Score = value;

}

}

}

}

// Event handling class

Public class Referee

{

Public Referee (Game game)

{

// Monitor score changes in game

Game. ScoreChange + = new ScoreChangeEventHandler (game_ScoreChange );

}

// Note that the method signature must match the ScoreChangeEventHandler method signature.

Private void game_ScoreChange (int newScore, ref bool cancel)

{

If (newScore <100)

System. Console. WriteLine ("Good Score ");

Else

{

Cancel = true;

System. Console. WriteLine ("No Score can be that high! ");

}

}

}

File: // TestTest Type

Public class GameTest

{

Public static void Main ()

{

Game game = new Game ();

Referee referee = new Referee (game );

Game. Score = 70]

Game. Score = 110; // [Note: No Score can be that high !]

}

}

In GameTest, we created a game and a referee monitoring game respectively. Then, we changed the Score of the game to see how referee reacted to the game. In this system, game does not have any knowledge about referee. Any class can listen to and respond to game score changes. The keyword event hides all delegate Methods except + = and-=. These two operators allow you to add (or remove) Multiple event processors that process the event.


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.