Home > Backend Development > C#.Net Tutorial > Events and delegates in C#

Events and delegates in C#

WBOY
Release: 2023-09-06 20:33:15
forward
1327 people have browsed it

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;   
Copy after login

Events can hold delegates like this.

this.TestEvent += new Demo (DemoData);
Copy after login

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);     
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template