C# and .NET Basics

Lambda Expressions

Spencer Schneidenbach

Spencer Schneidenbach

Aviron Software, Microsoft MVP
C# and .NET Basics

Check out a free preview of the full C# and .NET Basics course

The "Lambda Expressions" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:

Spencer introduces the concept of Lambda expressions or Lambda functions. He explains that Lambda functions are a shorthand way of defining small functions and demonstrates how to define and use Lambda functions in C#. Spencer also discusses the use of specialized types like func and action to define the shape of functions.

Preview
Close

Transcript from the "Lambda Expressions" Lesson

[00:00:00]
>> Spencer Schneidenbach: So, I want to talk a little bit about something called a lambda expression or lambda function, they're basically little short-form functions that you can define. And the implications of this are really apparent when you get to when we get to talking about link. So, this is an example of a lambda function.

[00:00:18]
So essentially what you've this is, it's basically a shorthand for defining a very, very tiny function. So, I basically say that I am defining a function that takes in two parameters and returns those parameters added together. And assuming that you declare it as a funk of int int int, which I'll talk about that in a second.

[00:00:39]
It is functionally equivalent to this right here, but it saves a lot of words. It saves a lot of keystrokes. So, this is referred to as a I mentioned that, this is a function that is a funk of, of, of. And so this is a specialized type that's used in a lot of the parts of the .net core libraries to define a function that has a specific shape.

[00:01:06]
So for instance, I can take this and I can say func, int, int, int,
>> Spencer Schneidenbach: And then set it to add. Oops, I did not copy paste it.
>> Spencer Schneidenbach: Boom, boom, perfect. And what I've done is I've defined a function in line inside of a variable, which looks a little weird.

[00:01:36]
I've defined a function add that takes in one two arguments, the first one being an int, the second one being an int, and it returns an int, okay? So it's a little bit of a funky syntax, just wait, gets better. This is functionally equivalent to this function right here, but everything besides this weird syntax here, everything to the right looks like it's saving a little space.

[00:02:03]
So this somebody talked about, asked about, earlier, about callbacks. And basically, and my interpretation of that was, what about taking in functions as arguments inside of our methods. So that's something that is a core part of .net. It's something that you do a lot of, especially when you're operating on collections.

[00:02:24]
The important thing to note is that, we don't have to be verbose when we define the shape of a function if we know what that function is supposed to look like ahead of time. And that'll be apparent in the next section. So two different types of functions. There's func, AKA a function that returns a value, and then there's action.

[00:02:46]
An action is something that doesn't return a value. So you can have, for instance, let me delete this. Let me do elete this, you can have an action.
>> Spencer Schneidenbach: That doesn't return anything. That is what an action is. It's a function that returns void, it doesn't do anything.

[00:03:10]
>> Spencer Schneidenbach: doesSomething =, and then you can say an empty set of parameters. Because we don't have any goes into and then we could just console.writeline and just say hi or something, call a function or do some kind of thing. But this isn't returning a value because console.writeline doesn't return a value.

[00:03:30]
However, this because we know that this is a func of that returns an end the return statement is implied. So kind of interesting, little bit different syntax. You could also define actions that take parameters, so you could say right, and then you could say, console dot write line.

[00:03:53]
This is a this is a console dot write line that takes in a string and doesn't return. Return anything is an action of string, aka, string is the only argument to this function. So, this does have the shape of the action of string function, and you can call it just like any other function,
>> Spencer Schneidenbach: Which is pretty cool.

[00:04:17]
We're gonna run it. And we're gonna see that our test string does get returned. My main point in showing you all of this is to show you that there is alternate syntaxes for declaring functions. Sometimes you don't want to declare a whole function for a particular behavior property.

[00:04:34]
It'll make a lot more sense when we get to when we get to the next section. So one one thing about lambda functions that they have one property is that they close, their closures. They close over values outside of their scope. So in this case, we have a counter that we've declared outside the scope of our function.

[00:04:58]
And this counter is still holding this function is still holding this counter in memory, even though the scope of this function does not declare counter directly. So this is a really important property because it means that this can keep track of values that exist outside of it as long as you refer to those values.

[00:05:23]
It's more of a kind of a warning or a behavior. In practice, you don't have to close over functions too often, unless you're declaring something like a local function, which is a function that's declared inside of a function, right? Like this void, IncrementCounter, counter++, right? You can do that, it's called a local function.

[00:05:49]
This also closes over the counter variable, and that just means to say that this function has access to it and it doesn't get a new copy. It gets the copy of counter as it exists today, even if you call function far outside of the scope. If you passed this function to another method to be called, it will get called.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now