Skip to main content

Deferred execution in C#

A lot of times, fellow developers ask me about yield return keyword and what this keyword is for.
This keyword allows us to iterate over enumerable collections in a more efficient way. Basically what does is tells the compiler do not execute this code unless is strictly necessary (for this reason it’s also called lazy evaluation).
I wrote a little sample that shows the keyword in action. The purpose of this code is to convert a DataTable into a Collection of objects. Maybe the algorithm is not the best way to accomplish the job but clearly shows how yield return works.
To see the advantages of lazy evaluation we first should take look at how the code will work if we don’t use it.
Here I convert a DataTable that contains four rows into a collection of objects and take the first element from the result. This version of convert works eagerly.

And the result is:


As we can see this in the output window, this version of the convert method walks thru the whole rows collection converting each row into an object even when we only need the first one. With a large set of data or expensive convert operation this method will perform poorly.
A better approach will be creating a method that is smart enough to not convert the whole data set when we only need the first element. This version could look something like this:

When we execute this method,  we get the same result that we got before but this time we don’t walked over the whole collection of rows, when we find the first element we just stop enumerating and return the result. In this case, the second implementation performs better than the first one, saving a couple of CPU cycles and memory consumption.


This is just a sneak pick of how deferred execution works in C# and the power that LINQ brought to the .NET Framework. 

Comments

Popular posts from this blog

How to create MS Word documents from Office templates using C#

The OpenXML SDK allows you to do pretty much anything you want with office files such as Excel, Word, etc… While many people like this library, I found it complex, unintuitive and poorly documented, not to mention the awful xml format that uses under the hood to represent the documents, styles, etc. So I decided not to use it and build my own solution. If you, like me, don’t like that library, you will find in this post an alternative approach to build word documents from templates using c#. A neat trick to work with Office is to use the macro recorder to understand how things work. The macro recorder allows you to start a macro, do something by hand, stop it, and then take a look at the generated VBA code. Once you do this, you are pretty much set. This is how it looks the template I’am going to use. Note: save the file as a Word template (.dotx) This is the code to create Word documents from C#: By running the code, you should get a document that looks...

Printing html using the embedded web browser control

In this post I’ll try to answer some questions about the web browser control and provide some workarounds for known issues involved in the printing process. I'm assuming that you have some experience with the web browser control and basic knowledge of COM and hosting APIs. So I’m not going to cover those topics. At the bottom of this page I’ve added the links to download a small library I wrote that takes care of printing HTML and a demo app so you can try it out without having to write any code by yourself. Using the code The HtmlPrinter class will allow you to print html from an URL or just passing the html as string, you can also specify the title and the number of copies you want to print. The code may look something like this: Now that we know how to use the API let get answer some questions. Why my app crashes when I try to print multiple copies of a page? Well, apparently when you send a lot of print commands to the web browser control, ther...

How to replace text with images on Word documents using C#

This post it’s a reply to a question I got from a previous post that shows how to work with Word templates from C# code . If you haven’t read it, I recommend to do so because I’m not going into details here. But basically it was about how to create a Word document from a template and perform same text manipulation. So, the question from Marcel Kieboom  was “Do you know if it would be also possible to replace one of the words with an image which is locally stored?” The answer is yes, and this is how you can do that. Based on the same convention I had used in the previous post, I should have a template like this: What I’m trying to do here is replace the text [angus-young] for an actual pic of Angus. The technique I’m using it’s pretty common on web sites and basically consist in have an image with a matching name for each keyword I want to replace and then create the image path dynamically. This is the C# code to do that. * When I execute t...