https://github.com/strypper/eventhandleranddelegate
This is a small demo to demonstrate how to use EventHandler and Delegate, for more detail see Mosh explain at https://youtu.be/jQgwEsJISy0
https://github.com/strypper/eventhandleranddelegate
console csharp delegate event-handler
Last synced: 12 months ago
JSON representation
This is a small demo to demonstrate how to use EventHandler and Delegate, for more detail see Mosh explain at https://youtu.be/jQgwEsJISy0
- Host: GitHub
- URL: https://github.com/strypper/eventhandleranddelegate
- Owner: Strypper
- Created: 2020-04-26T10:38:21.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-04T05:46:05.000Z (over 1 year ago)
- Last Synced: 2025-02-27T03:11:51.413Z (about 1 year ago)
- Topics: console, csharp, delegate, event-handler
- Language: C#
- Size: 452 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Event Handler and Delegate in C#
For what ?
* Loosely couple architecture
* Easy to test
# What is in the code ?
There are 2 type of Event Handler, the one with no parameter and the one with code inside it
# No Parameter
* Create a delegate
* Create an Event Handler based on that delegate
* Raise the Event

# With Parameter
* Create a delegate but with a class which inheritances a custom EventArgs as second parameter
* Create an EventHanlder base on the above Delegate
* Require the trigger method with the parameter we need
* Raise the Event

# Better Way
In the new C# version we don't need to do this
```cs
public delegate void VideoEncodedHandler(object source, VideoEventArgs args);
public event VideoEncodedHandler videoEncoded;
```
Instead just to this
```cs
public event EventHandler videoEncodedWithParameter;
```
#Execute the example
```cs
static void Main(string[] args)
{
//Event with no Parameter
Video v = new Video("Family Guy");
var VE = new VideoEncoder();
var SE = new SendEmail();
var SS = new SendSMS();
VE.videoEncoded += SE.OnVideoEncoded;
VE.videoEncoded += SS.OnVideoEncoded;
VE.EncodeVideo(v);
//Event with Parameter
Video v1 = new Video("Fuck you Peter");
var VEP = new VideoEncoderWithParameter();
var SEP = new SendEmailWithParameter();
var SSP = new SendSMSWithParameter();
VEP.videoEncodedWithParameter += SEP.OnVideoEncoded;
VEP.videoEncodedWithParameter += SSP.OnVideoEncoded;
VEP.EncodeVideo(v1);
}
```