SIGN UP MEMBER LOGIN:    
ARTICLE

Lambda Expressions with Multiple Parameters

Posted by Jean Paul Articles | C# Language May 01, 2011
Lambda Expressions are getting good recognition among developers and it would be nice to have the next level of information regarding it.
Reader Level:
Download Files:
 

Lambda Expressions are getting good recognition among developers and it would be nice to have the next level of information regarding it.

Summary

As you know, Lambda Expressions is an anonymous function that can contain expression or statements. The lambda expression uses the => (goes to) operator.

Example of One Parameter Lambda

var result = list.Where(i => i == 100);

Example of Two Parameter Lambda

var result = list.Where((i, ix) => i == ix);

Code Explained

In the above case the Where() method is having two overloads and we were using the second overloaded method which takes Func<T, T, bool> as argument.

Lamba1.gif

The first argument is the element itself and second would be the index. The value of index will be passed by the caller.

Applications of Index Parameters

It could be of rare applications where we needed the lambda index parameter. I have seen in many interviews they ask scenarios based on it. Some of the scenarios are given below.

Scenario 1

You have got a list of random integers. We need to find the numbers whose value equals to the index. How to achieve this in one line?

Setup Code

The following code can be used for creating the main list.

IList<int> list = new List<int>();

list.Add(0); // Value = Index
list.Add(2);
list.Add(2); // Value = Index
list.Add(1);
list.Add(4);
// Value = Index


One Line Lambda Solution

var sublist = list.Where((i, ix) => i == ix);

The Lengthy Solution

var newList = new List<int>();
int jx = 0;
foreach
(int j in list)
    if (jx++ == j) // Check index and increment index
        newList.Add(j);

From the above code we can see without lambda expressions the solution will be taking more than 1 lines.

Result

We can see the associated result of the console application. The source code is attached with the article.

Lamba2.gif

Summary

In this short article, we have seen the advantage of Index parameter in Lambda expressions in reducing the code and increasing the speed of development as well as execution.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor