Author avatar

Dániel Szabó

Closures and the Lambdas

Dániel Szabó

  • Sep 16, 2019
  • 5 Min read
  • 96 Views
  • Sep 16, 2019
  • 5 Min read
  • 96 Views
Languages Frameworks and Tools
C#

Introduction

In C#, the capability that allows a method or a function to reference a non-local variable or value is called closure. This feature is commonly utilized in conjunction with the lambda functions which are nothing more than anonymous functions that developers utilize over named function to provide a way to add flexibility. This guide will make the terms of lambda functions and closures clear, and also demonstrate the capabilities of these very important features.

Non-local Variables

In C#, accessing a variable outside the scope of a function is possible and considered normal. Variables accessed this way are called non-local variables. You may also hear developers referring to this type of variable as captured variable. They refer to the same type of variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
class LambdasNClosure
{
	static string platform = "Pluralsight";
    static void WhoIsTheBest()
    {
    	Console.WriteLine($"{platform} is the best!");
    }
    static void Main(string[] args)
    {
        WhoIsTheBest();
        Console.ReadKey();
    }
}
csharp

Calling the function gives us the following results.

1
Pluralsight is the best!
bash

This demonstrated the concept called closure.

Lambda Functions

There are lots of articles about lambdas being closures and some people tend to agree that they are only part of the truth. I strengthen the camp of those in the opposition. Lambda functions may be implemented as closures, but they are not closures themselves. This really depends on the context in which you use your application and the environment. When you are creating a lambda function that uses non-local variables, it must be implemented as a closure.

Lambda expressions can basically take two forms.

  1. Expression lambdas
  2. Statement lambdas

Expression lambdas usually look like this:

1
Func<double, double, bool> isItEqual = (x,y) => x == y;
csharp

Technically, we define the input and output types and provide an expression which is evaluated based on the argument, then it is returned as a result. Sometimes the compiler cannot infer the input types, so you may want to be on the safe side and specify them like this.

1
Func<double, double, bool> isItEqual = (double x, double y) => x == y;
csharp

Statement lambdas usually look like this:

1
2
3
4
Action<string> Welcome = name =>
{
	Console.WriteLine($"Welcome {name} to the Pluralsight platform!");
};
csharp

Note how the closing curly brace also ends in a semicolon. When we work with statement lambdas, we usually create multiple expressions - encapsulate them inside our function. These are the best practices for creating an atomic operation; they exist for a single purpose/action.

Now we take a sharp turn and combine lambdas and closures.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static partial class ClosureDemo {
	public static void Outside()
		{
			string notLocal = "Closure based lambda!";
			Func<string> demo = () =>
			{
				string local = "This is lambda, ";
				return local + notLocal;
			};
			string message = demo();
			Console.WriteLine($"The burrowed message: {message}");
		}   
	}
class LambdasNClosure
	{
	static void Main(string[] args)
	{
		ClosureDemo.Outside();
		Console.ReadKey();
	}
}
csharp

The output is as follows:

1
The burrowed message: This is lambda, Closure based lambda!
bash

In this example our non-local variable did not change, however, this situation might not always be true. If the variable changes, the referencing function will be impacted. Sometimes this is exactly the case we want to achieve. For example, we could have a function that logs the internal change in a function's state with a lambda function. That way, each change to the state will result in a different log.

Closures

Under the hood closure is nothing more than a syntactic sugar that allows access to a non-local variable. This is very convenient, however, it comes with a price. Another wording for closure which is fairly common is that it's a block of code which maintains its environment and can be called at a later point in time.

Let's demonstrate it with a tiny code.

1
2
3
4
5
6
7
8
9
static void Main(string[] args)
{
    int foo = 10;
    Func<int> Bar = () => { return foo; };
    Console.WriteLine($"Foo: {Bar()}");
    foo = 20;
    int fighter = Bar();
    Console.WriteLine($"Fighter: {fighter}");
}
csharp

The output shows the following results:

1
2
Foo: 10
Fighter: 20
bash

Conclusion

Closures can be underwhelming, as alone they do not seem to provide too much extra functionality or gain. Their power becomes apparent only when you combine them with libraries which take advantage of their nature. Letting you express specific behaviors at the right places. I hope this was informative for you and that you found what you were looking for.

0