Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of events supported by C#

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "C# support event example Analysis". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Here we introduce that C# supports events (which is also a feature of MSVJ). At present, many mainstream programming languages handle events in different ways. Delphi uses function pointers (which is the term "closure" in Delphi), Java uses adapted classes to implement, VC uses WindowsAPI's messaging system, and C# uses delegate and event keywords directly to solve this problem. Let's take a look at an example that gives you the whole process of declaring, invoking, and handling events.

/ / first is the declaration of reference, which defines the event signal public delegate void ScoreChangeEventHandler (int newScore, ref bool cancel) that wakes up a function; / / defines a class public class Game that generates events {/ / Note that the event keyword public event ScoreChangeEventHandler ScoreChange; int score; / / Score attribute public int Score {get {return score;} set {if (score! = value) {bool cancel = false is used here. ScoreChange (value, ref cancel); if (! Cancel) score = value;} / / the event handling class public class Referee {public Referee (Game game) {/ / the referee is responsible for adjusting the score changes in the game game.ScoreChange + = new ScoreChangeEventHandler (game_ScoreChange);} / / notice how the function here corresponds to the private void game_ScoreChange (int newScore, ref bool cancel) {if (newScore < 100) System.Console.WriteLine ("Good Score") of the ScoreChangeEventHandler signal. Else {cancel = true; System.Console.WriteLine ("No Score can be that high!");} / / main function class, used to test the above features public class GameTest {public static void Main () {Game game = new Game (); Referee referee = new Referee (game); game.Score = 70; game.Score = 110;}}

In the main function, we create a game object and a referee object, and then we change the game score to see how the referee responds to this.

This is the end of the content of "C# support event example Analysis". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report