Let’s talk about something that you should not encounter in your day to day programming experience, but nevertheless, you should be aware of, if you want to become a professional engineer: covariance and contravariance with delegates. They are general programming terms, so, you will encounter them in other programming languages as well, not just C#.… Read more
Archive for the ‘Delegates, Lambda Expressions, Events’ Category
Delegate covariance and contravariance
Wednesday, March 11th, 2020EventHandler, sender and EventArgs
Friday, February 21st, 2020Events Add and Remove
Wednesday, February 19th, 2020In the previous lesson I showed you how the compiler actually implements events by actually adding two methods named addon() and removeon() in the background and making the Action field private, so we can’t invoke it. But that was done in MSIL language, and we really don’t need to deal with such a low level.… Read more
Events
Wednesday, February 5th, 2020Events are a more secure way of implementing the observer pattern described in the previous lesson, and they are the evolutionary step of raw delegates. You may have heard about the event-driven programming as a concept that describes a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs or threads.… Read more
Observer Pattern
Monday, February 3rd, 2020According to Microsoft, observer pattern is a behavioral design that allows one object to notify other objects about changes in its state.
Many beginner programmers (and even more experienced ones) have a hard time understanding the link between delegates and events, and the core upon which this link is built is represented precisely by the observer pattern.… Read more
Closures
Sunday, February 2nd, 2020Let’s consider the following Action:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace HelloWorld { public class Program { public static void Main() { int i = 0; Action a = () => i++; a(); a(); a(); Console. |
Anonymous methods
Saturday, February 1st, 2020Remember from the lesson lambda expressions that we can declare a method locally, without a name, and use it only in one place, where we declare it. This is an example of a lambda expression:
Func and Action
Wednesday, January 29th, 2020There are five builtin delegate types that you can use in C#: Delegate, MulticastDelegate, Predicate, Func and Action. I’ve already described Delegate in a previous lesson. MulticastDelegate is there only for historic reasons, and it allows us to chain up delegates, but you will rarely or never use it directly.… Read more
Delegate chaining
Friday, January 24th, 2020An useful property of delegate objects is that multiple objects can be assigned to one delegate instance using the + operator, process called delegate chaining.
Delegate chaining isn’t really useful until we will get to events and event subscribers, which will come in a future lesson, but it’s better to describe the behavior now, after you’ve seen a bit of delegates inner workings.… Read more