Friday, May 14, 2021 02:08

Yield break statement

May 11th, 2021

Whenever we use the yield keyword in a statement, we indicate that the method, operator, or get accessor in which it appears is an iterator. Of course, since we know that iterators are used to… duh! iterate on collections of data, and since we know that when iterating on a collection, we can use the break keyword to immediately terminate the iteration, it is only obvious that whenever we use an yield return statement to return values in an iterator, like I’ve shown in the previous lesson, we can also use yield break to terminate the iteration of the said iterator.… Read more

Yield return statement

February 22nd, 2021

In the previous lesson, I have talked about IEnumerable and IEnumerator, and how they help us when we need to iterate over collections of data. Let’s take an example that deals with those concepts:

C# IEnumerable

We have a function, GenerateRandomNumbers(), inside which we declare a List of ints, generate a number of random ints, equal to the _count parameter, add those random ints to the list, then return the list.… Read more

IEnumerator and IEnumerable

May 8th, 2020

Before we can start delving into LINQ, we need to first understand the underlying principles upon which it is built. LINQ is all about operations over collections, so, you’ve guessed it: we will be dealing with collections.

You already learned that of all data structures, arrays are the fastest, because they are unsorted non-generic data structures.… Read more

Extension methods

May 4th, 2020

Sometimes, programmers find themselves in need of adding new functionality to already existing codes, in order to improve or complete them. If the said source code is available, the task is simple – they only need to add the required functionality and recompile.… Read more

Delegate covariance and contravariance

March 11th, 2020

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

EventHandler, sender and EventArgs

February 21st, 2020

You noticed that throughout pretty much all the previous lessons where I discussed events, I have been using Action as the delegate type for the event. Obviously, you can use whatever delegate type you’d like, but in the vast majority of the cases, by convention (and only by convention!)… Read more

Events Add and Remove

February 19th, 2020

In 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


February 5th, 2020

Events 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

February 3rd, 2020

According 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


February 2nd, 2020

Let’s consider the following Action:

Read more

Follow the white rabbit