Click here to Skip to main content
13,191,400 members (73,729 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

12.5K views
22 bookmarked
Posted 30 Sep 2015

Understand Lambda Expressions in 3 Minutes (Continuation)

, 30 Sep 2015
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Understand Lambda Expressions in 3 minutes"

I like the Original-Tip very much, because it is short and clear, and what is explained is well explained. Please refer to it, otherwise this one is incomplete.

But I miss some important topics, that the article really meets its title "Understand Lambda..."

These are:

  • Lambdas can be assigned to variables
  • Can have several lines
  • Can have several parameters
  • Can be void
  • Can have return-Statement

So here is a short glimpse how these mentioned features can look like:

  1  void foo() {
  2     List<int> numbers = new List<int> { 11, 13, 37, 52, 54, 88 };  // data
  3  
  4     // original - sample
  5     List<int> oddNumbers = numbers.Where(n => n % 2 == 1).ToList();
  6  
  7     // original - lambda, assigned to a variable
  8     Func<int, bool> isOdd = n => n % 2 == 1;  
  9     oddNumbers = numbers.Where(isOdd).ToList();
 10  
 11    // complex sample, assigned, with lines, params, void, return-statement
 12     var output = new List<string>();
 13     Action<int, int> tryCreateLine = (i1, i2) => {
 14        var result = i2 - i1;
 15        if (result < 10) return;   // reject create the line
 16        output.Add(string.Format("{0} - {1} = {2}", i2, i1, result));
 17     };
 18     for (var i = 1; i < numbers.Count; i++) tryCreateLine(numbers[i - 1], numbers[i]);
 19     MessageBox.Show(string.Join("\n", output), "calculations with result > 10");
 20  }

The Messagebox-Output is as given below:

calculations with result > 10
---------------------------
37 - 13 = 24
52 - 37 = 15
88 - 54 = 34

I don't explain the code - it is self-explanatory. And you will recognize and understand each of the missing topics mentioned above.

You see: Lambdas support the complete C#-power - if wanted, one could write complete programs without any "normal" function.
But as the Original-Author already said:

Lambda expressions should be short. A complex definition makes the calling code difficult to read. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mr.PoorEnglish
Germany Germany
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionAction Delegate does not return a value Pin
Jimmy_James_J2-Oct-15 8:05
memberJimmy_James_J2-Oct-15 8:05 
AnswerRe: Action Delegate does not return a value Pin
Mr.PoorEnglish2-Oct-15 8:26
memberMr.PoorEnglish2-Oct-15 8:26 
GeneralRe: Action Delegate does not return a value Pin
PHenry2-Oct-15 10:21
memberPHenry2-Oct-15 10:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171017.1 | Last Updated 1 Oct 2015
Article Copyright 2015 by Mr.PoorEnglish
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid