Advanced Usages of Events

In this post, we’ll explore more advanced concepts and techniques related to C# events. These advanced topics will help you become a proficient event handler and enable you to use events in complex scenarios. Custom Event Arguments While events typically use the built-in EventArgs class to convey event information, you can create custom event argument classes to pass additional data to event handlers. Custom event arguments allow you to provide specific context to subscribers. For example, in a file monitoring application, you might create a custom event argument class to include the file name and the type of file change. ...

November 3, 2023 · 5 min · 1020 words · Kamran Sadin

A Guide to EventHandlers and Delegates

In the previous post, we introduced the basic concepts of C# events. Now, let’s dive a bit deeper into event handling and delegates, which are fundamental to working with events in C#. Event Handlers An event handler is a method that gets executed when an event is raised. In C#, event handlers are defined with a specific signature, which typically includes two parameters: object sender: This parameter refers to the object that raised the event. EventArgs e: This parameter holds additional information about the event. In many cases, a more specific event argument class is used to convey relevant data. Here’s a simple example of an event handler: ...

November 1, 2023 · 4 min · 746 words · Kamran Sadin

Delegates Part 2

Delegates Part 1 Delegates Part 2 Prerequisits You want to read the second part of Delegates, right? The first thing that you need is to read the previous part (Part 1). The next thing is putting a smile please put a smile on your face then start to read the article. Target methods As you remember, we agreed that a delegate can point to a method, right? In simple words, it means that we can have a variable that can hold a method in it and then we can call that method by calling this variable. We have local, static, and instance methods in C#, so the delegate can point to which of them? All! A delegate’s target method can be a local, static, or instance method. ...

October 4, 2023 · 9 min · 1726 words · Kamran Sadin

Delegates Part 1 Plugin Methods With Delegates

Delegates Part 1 Delegates Part 2 This post is just to get familiar with Delegates in C#, in the next posts I will talk about delegates in advance. What is Delegate A delegate is an object that knows how to call a method. {: .prompt-tip } You know variables in C#, right? A delegate is a reference type variable that holds the reference to a method and the reference can be changed at runtime. Delegates are usually used for implementing events and the call-back methods (We will get back to this soon). Before getting started, let’s see the syntax of a delegate: ...

October 1, 2023 · 4 min · 754 words · Kamran Sadin