Wednesday, December 04, 2024 22:28

LINQ execution workflow

June 23rd, 2021

In the previous lesson, I talked about the fact that LINQ actually delays the execution of its constituent queries up until the very last moment, when we actually need the data. What was not as obvious at the time was the order in which the LINQ queries are executed.… Read more

Deferred execution

June 9th, 2021

A lot of people seem to think that deferred execution exists because of LINQ, and that is only partially true. LINQ does have a lot to do with it, but deferred execution exists first of all because of the yield statement.… Read more

Degenerate Select clauses

June 7th, 2021

Taking an excerpt code from the previous lessons about LINQ, we had these codes:


If you paid attention to the above code, you probably noticed the Select(n => n) function. If you analyze it a bit, you realize that it’s a lambda expression that all that it does is to take a parameter named n and return it, without any modification.… Read more

Writing custom LINQ extension methods

June 7th, 2021

In the previous lesson, when I introduced you to a preview of what LINQ looks like, I ended up with this code:


And we learned that the free form syntax of LINQ is the same thing as the declarative syntax of LINQ, which is also the same with some bunch of extension methods for the IEnumerable interface, declared inside a class called Enumerable.… Read more

LINQ

May 24th, 2021

Finally, we now know enough to start talking about LINQ, which is an acronym for Language Integrated Query which is basically just a fancy way of doing queries and SQL-like things in C#. If you don’t know what SQL is, you should probably research about it a bit first, but, at a macro level, SQL (Structured Query Language) is just a language for interacting with databases.… Read more

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

The meaning of “this” on extension methods

May 4th, 2020

In the lesson about extension methods, we learned that they are a nice way of adding extra functionality to existing types, and we did that by adding a static method inside a static class, that took a type parameter prefixed with the this keyword, like this:


So, although my original Book class does not contain a method called SellBook(), I am able to call it inside the Main() method because I declared an extension method for the Book class, and this static SellBook() method took a Book type parameter prefixed with the this keyword, that signals the compiler that the method is intended as an extension method for the Book type.… 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


Follow the white rabbit