Becoming a software developer – episode IV

Becoming a software developer – episode IV

Welcome to the fourth episode of my course “Becoming a software developer”, which will give you a quick overview of the more advanced concepts of the C# language that allows writing a really neat and composable code.

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


 

Scope

This episode will focus on the the very interesting parts of the language:

  • Delegates
  • Lambda expressions
  • Events

Abstract

Delegate is a sort of pointer to the method. Do you remember the interfaces (well, you should)? By using delegate you can also specify such form of “contract”, but in this case, it points to the method. Consider the following example:

So, what happened here? At first there’s a delegate named “Add” – it has its unique name, type that is being returned (you can also use void) and the method parameters (0 or more). Below is some typical method with its body. And finally, we’re assigning our variable adder being our Add delegate to the AddTwoNumbers method. And why we can do that? Because, both, the type that is being returned int and the method parameters int and int do match. By having such adder variable we can invoke (or call) it as it actually points to our method and will result in adding two numbers. It’s not that complicated right?

Now, what might be the real use of this weird concept? Well, what about passing the as the arguments to our methods? Yes, you hear me right – you can pass a method to the method and in some languages (like JavaScript) it’s called that methods are treated as “first class members”, because we can assign them to the variables, pass as arguments to other methods (it’s called composition) etc. Soon, you will see why it’s one of the most powerful concepts. It’s so powerful, that there are a whole paradigm and a big family of so-called functional languages that are built on this idea.

Lambda expression is the evolution of delegate. You can declare the anonymous methods that can do pretty much anything (under the hood they’re a normal methods with input parameters and a type that is being returned). Take a look at this:

What happened here? We’ve declared an which is a special type that takes 0 (if you don’t want to use any method parameters) or more types specified using previously known generic approach. Action does not return anything (it’s void) while on the other hand the Func behaves pretty much in the same way with the only difference being the last type that determines what should be returned (so it’s like a delegate that is not void, but in our sample a string).
Let’s take a look at the more sophisticated examples.

Treating functions as the beforementioned first class members and being able to declare them anonymously using lambda expressions is one of the most powerful features of the C# language. It can be used for filtering the collections, transforming objects between different types, creating custom validation rules and so many other great things, that literally, sky is the limit here. Trust me on this and I strongly encourage you to play with this concept for a long time, in order to truly understand it. Then it will become a natural pattern that you’ll be able to apply whenever it makes sense.
And these special objects are pointers to the functions, just like the delegate – you can see the beauty and simplicity of using such inline and anonymous methods?
If you’re still uncertain what is the practical use of this concepts, let me give you one of the many examples that you will discover in the next episode:

Events

The last concept that we’re talking about will be events. It’s a special keyword that you can use in order to make use of the events that may occur within your application.
Events are of course connected to the delegates, and there’s a special type EventHandler which is a particular delegate returning void (nothing).
We’re not going to use them too often (if at all) within the web application as they’re stateless (unlike the desktop application, where the events are heavily used).

Resources

Next

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

In the next episode, we’ll dive into the LINQ and make use of our anonymous methods and objects in order to make the sophisticated queries for filtering and transforming collections of data. Eventually, you’ll see the difference between the IEnumerable and IQuerabyle and what is all the fuss about the yield keyword.

6 Comments Becoming a software developer – episode IV

  1. Pingback: Becoming a software developer – episode IV – Patryk Huzarski | Personal Blog

  2. Pingback: Dew Drop - February 16, 2017 (#2423) - Morning Dew

  3. Fellow dev.

    This is awesome Piotr. I am an English speaking audience and loved reading your write-up. I’m feeling encouraged to play around with delegates more – in more situations than just events. Keep it up!

    Reply
    1. Piotr Gankiewicz

      Thank you very much! I wish you could watch the videos, yet we’re recording them using the Polish language in order to make sure that our local community gets the most benefits out of it. However, maybe one day I’ll be able to include subtitles or record them again in English, so they could provide valuable content for you and other non-polish speakers :).

      Reply
  4. Kuba

    Hi Piotr ! When you talk about lambda expressions, there are (in code) methods which you invoke as “sayMyName” and “sayMyNameAgain”. Shouldn’t be there “showMyName” and “showMyNameAgain”? Anyway, perfect job!

    Reply

Leave A Comment

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