Becoming a software developer – episode V

Welcome to the fifth episode of my course “Becoming a software developer”, which will guide you through the concept of enumerators and how to deal with the collections.

All of the materials including videos and sample projects can be downloaded from here.


 

Scope

This episode will focus mostly on the IEnumerable type and all that it’s related to it:

  • Debugging in a nutshell
  • Extension methods
  • IEnumerable
  • yield
  • IQueryable
  • LINQ

Abstract

Debugging is a special form where you can run your code in a so-called debug mode that lets you set the breakpoints and navigate through your code while running the application. You can do a lot of useful things such as choosing which lines of the source code would you like to go to, preview the values of the variables, change them dynamically while running the application and so on. Debugging is mostly used for removing bugs from the application (this is how it got his name), yet it’s not its only task, as it’s very useful in the other areas as well. You can run your code in the debug mode by hitting F5 and set (add or remove) breakpoints by hitting F9 (also by clicking next to the line number). From that point, you can jump between the next lines of the source code for example by clicking F10 or by next breakpoints by clicking F5. To get most out of the debugging you should use the full version of Visual Studio or Project Rider as these two possess the most advanced features. On the other hand, the Visual Studio Code allows to debug your code, but without many special functions which are not available here (but to be honest not needed most of the time either).

Extension methods are a syntactic sugar which allows writing the code in a more fluent manner, just consider the following example.

Can you see what happened? You can invoke the extension method on any type and it can return any value or nothing which would be void. It feels like calling the method that would naturally be a part of the selected type although under the hood it’s just a static method invocation. That’s kind of neat, isn’t it?

IEnumerable is a special interface based on the concept of enumeration. You can think about it as a behavior that allows you to iterate (via foreach which is merely a syntactic sugar over the while loop) through the collection and it’s up to you whether you’d like to extend it. For example, you can use plain IEnumerable for the read-only collections, while the IList or ISet would be a special implementation of such behavior which lets to modify the elements of the collection. The important thing here is to realize that it’s a sequence, which means that you can iterate through its elements one by one, in theory, you could e.g. generate an infinite sequence of the numbers or so. The compiler doesn’t care about how many elements there are, as it doesn’t have to load all of them into the memory like it does while using e.g. List. It’s a very useful feature that shines especially later on once you’ll get familiar with the concept of lazy or deferred loading.

Yield is a special keyword used along with the IEnumerable. By using the yield return you can return the next element of the sequence. Simple as that, just take a look at the sample below.

IQueryable is of type IEnumerable which means that you can achieve pretty much the same things here, however, it’s very significant to understand why this separate interface was introduced in the first place. Just remember the following – whenever you see the usage of IQueryable you can be more than certain that you’re dealing with the external call to the data provider (e.g. a database) and all of the invocations based on that interface will be translated for example into the SQL. Thus, you have to very careful here as for example running another foreach loop will result in making another call to the database and it could be a costly operation taking a lot of time and computing resources. Make sure that you use some greedy operator at the end of your query e.g. ToList() and you can do whatever you want with your collection, as it will be loaded into the memory and filtering it further will not call your database anymore

LINQ stands for Language Integrated Query and is a powerful tool which was built mostly as a set of the extension methods that allow filtering, grouping, aggregating, sorting and performing many, many other interesting operations on the collections. You can use either the regular method chaining with lambda expressions or another syntactic sugar equivalent which looks more like the SQL queries. The important thing to remember is that these methods never modify your collection, instead, they return a brand new collection that you can e.g. assign to some variable or keep on processing until you get your results. You can easily play with LINQ by using LINQPad.

Resources

Next

  • Reflection
  • Dynamic
  • Attributes
  • Async & Await
  • Parallelism

The sixth episode will be the last one in which we will talk about the features of the language. Of course there are more of them, however, I chose the ones that I found to be the most important ones, especially in terms of writing our application during the course in the near future.

8 Comments Becoming a software developer – episode V

      1. zawisz

        Super, nie mogę się doczekać!

        A jak planujesz prowadzić projekt aplikacji? Repo, testy, DI, jakiś framework do bazy, wzorce itd (czyli wszystko na mega wypasie jak kurs aktualnie). Czy raczej taka wersja bardziej light?

        Reply
  1. Pingback: Dew Drop - February 23, 2017 (#2428) - Morning Dew

  2. Johny

    Witam!

    Super seria kursów.
    Wielki szacun za to co robisz dla community początkujących programistów ale także takich jak ja (4 lata doświadczenia ale w innym języku i w firmie w której się tylko wytwarza bez dbania o jakość) . Dzięki tobie nabrałem motywacji do własnego pet project oraz do zmiany ścieżki kariery.

    Pozdrawiam

    Reply
    1. Piotr Gankiewicz

      Cześć,
      Dziękuję, cieszę się, że się podoba :). Robię to z różnych powodów m.in. tego, że chcę się sprawdzić w roli nauczyciela, przekazać solidne podstawy, które są często pomijane w tego typu kursach (bez przełożenia na rzeczywiste aplikacje) i też w jakiś sposób się rozpromować.
      Super słyszeć, że nabrałeś motywacji :D.

      Pozdrawiam!

      Reply
  3. Pingback: Becoming a software Developer – episode V – Patryk Huzarski | Personal Blog

Leave a Reply to Piotr Gankiewicz Cancel reply

Your email address will not be published. Required fields are marked *