I have observed that Lambda Expressions got good steam among developers. It helps us in reducing lot of code and save time. I would like to list some of the useful Lambda Expressions here comparing with the without-lambda approach. Common Setup Code Here the common initialization code for using the below examples are given: List<int> list = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
list.Sort(delegate(int x, int y) { return y.CompareTo(x); });
bool result = true; foreach (int i in list) if (i >= 100)
bool result = false; foreach (int i in list) if (i == 5)
var sublist = new List<int>(); for (int i = 0; i < 5; i++) if (i < list.Count) sublist.Add(i);
List<int> result = new List<int>();
foreach (int i in list)
if (i < 8)
if (result.Count < 5)
Summary I think the examples above demonstrate how complex the code will look even if you do the simple sorting and selection. Surely Lambda Expressions is worth learning and in the long run it would give more manageable code. Note In the sorting scenario above, the original list is not updated, OrderBy() will be giving a sorted view of the original list. For assigning it back to the original list we have to use the ToList<int>() method as: list = list.OrderByDescending(i => i).ToList<int>();
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: