Home  >  Article  >  Backend Development  >  Events and delegates in C#

Events and delegates in C#

WBOY
WBOYforward
2023-09-06 20:33:151304browse

C# 中的事件与委托

C# events are used to solve troubles in delegates. One of them can easily override delegate properties, which can eventually lead to bugs in your code. To avoid this, C# uses events and defines wrappers around delegates.

Events in C

#To use events, you should first define a delegate. An event is a delegate type, pressing a key is an example of an event.

public delegate voide Demo(String val);     
public event Test TestEvent;   

Events can hold delegates like this.

this.TestEvent += new Demo (DemoData);

Delegation in C

#A delegate is a reference type variable that holds a reference to a method. References can be changed at runtime.

public delegate void Demo(String val);     

The above is the detailed content of Events and delegates in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete