- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Events vs Delegates in C#
C# events are used to resolve the hassles in Delegates. One an easily override Delegate properties and that can eventually lead to errors in the code. To avoid this, C# uses Events and defines wrappers around Delegates.
Events in C#
To use Event, you should define delegate first. Event is a type of Delegate and an example of event can be when a key is pressed.
public delegate voide Demo(String val); public event Test TestEvent;
An event can hold a delegate like this.
this.TestEvent += new Demo (DemoData);
Delegates in C#
A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.
public delegate void Demo(String val);
Advertisements