An introduction to lambda expressions in C#
Join the DZone community and get the full member experience.
Join For FreeAs I reviewed the usage of delegates in C#, those make the method access easier in the context of application extensibility since no method is explicitly declared - a delegate that is passed to a method or function can be a placeholder for another method or function.
In C#, there are basically three ways to pass a method to another method or function via a delegate – using explicit method naming, anonymous delegates or lambda expressions. To give a short example of all three methods, take a look at the code below.
public delegate string ShowConcatenation(string first, string second);
string Concatenate(string first, string second)
{
string res = first + second;
return res;
}
The Concatenate method is fitting the delegate signature, so I am going to use it in an event handler:
ShowConcatenation concatenation = new ShowConcatenation(Concatenate);
Debug.Print(concatenation("D","Zone"));
I am using no event handler declaration for this specific example, but obviously, you can use the code in any event handler or method/function.
The same result could be achieved by using an anonymous delegate that would basically be an inline method:
ShowConcatenation concatenation = new ShowConcatenation(delegate(string first, string second)
{
string res = first + second;
return res;
});
Debug.Print(concatenation("D","Zone"));
The same result will be achieved via lambda expressions, making the above code even shorter:
ShowConcatenation concatenation = new ShowConcatenation((first, second) => first + second);
Debug.Print(concatenation("D","Zone"));
As you see, there are two parts of a lambda expression – first of all, there is the parameter declaration – (first, second), and after that the returned expression – first + second. I did not explicitly declare what first and second are; therefore the result type will be determined depending on the values and operations that are done with the values passed. However, in my specific case I have restrictions imposed by the delegate – I can only use strings for the executing function and the return value is also a string. The limitations set by the delegate also affect the possible manipulations that can be performed with the passed values.
The parameters in a lambda expression can also be explicitly declared – I can use (string first, string second) for the parameter declaration.
Lambda expressions can also contain multiple statements enclosed in a declaration. Here is an example:
ShowConcatenation concatenation = new ShowConcatenation((first, second) =>
{
string result = first + second;
result += "!";
return result;
});
Debug.Print(concatenation("D","Zone"));
Lambda expressions can also have a void return type, although the usage of such expressions is quite limited. The implementation starts with the delegate declaration:
public delegate void CustomAction();
Notice the fact that I replaced the existing delegate with this one to adapt to the return value change – its return value is void. The actual lambda expression will look like this:
CustomAction action = () => { Debug.Print("DZone"); };
action();
I tried to cover the basics of lambda expressions in this article, but there is a lot more to add. The fundamental aspects outlined here should lay the foundation of the correct lambda expression understanding and it can get you started with their usage.
When using lambda expressions, my personal advice would be - don't over-use them. If used excessively, it affects the code readability and it will take a little bit longer for someone else to understand your code (or even for yourself after a period of time).
Opinions expressed by DZone contributors are their own.
Comments