DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

An introduction to lambda expressions in C#

Denzel D. user avatar by
Denzel D.
·
May. 04, 10 · Interview
Like (0)
Save
Tweet
Share
10.70K Views

Join the DZone community and get the full member experience.

Join For Free

As 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).

csharp

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • REST vs. Messaging for Microservices
  • Cloud Performance Engineering
  • Building a Real-Time App With Spring Boot, Cassandra, Pulsar, React, and Hilla
  • How To Choose the Right Streaming Database

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: