Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this "Lambda Expression" do?

Just come across the following line of code and having a hard time finding documentation for it, is it a lambda expression? What does this do?

temp = Regex.Replace(url, REGEX_COOKIE_REPLACE,match => cookie.Values[match.Groups["CookieVar"].Value]);

Specifically interested in the =>.

like image 337
m.edmondson Avatar asked May 17 '11 09:05

m.edmondson


People also ask

What is a lambda expression Python?

What is a Lambda Function in Python? A lambda function is an anonymous function (i.e., defined without a name) that can take any number of arguments but, unlike normal functions, evaluates and returns only one expression. A lambda function in Python has the following syntax: lambda parameters: expression.

What is lambda expression in C++?

C++ Lambda expression allows us to define anonymous function objects (functors) which can either be used inline or passed as an argument. Lambda expression was introduced in C++11 for creating anonymous functors in a more convenient and concise way.

What does => mean in lambda?

In lambda expressions, the lambda operator => separates the input parameters on the left side from the lambda body on the right side. The following example uses the LINQ feature with method syntax to demonstrate the usage of lambda expressions: C# Copy.

What is a lambda expression example?

Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable).


1 Answers

If you look at the documentation for Replace, the 3rd argument is a MatchEvaluator:

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx

This is a delegate that takes a Match as an argument and returns the string to replace it with. Your code is defining a MatchEvaluator using a lambda expression:

match => cookie.Values[match.Groups["CookieVar"].Value]

Here, for each match that the Regex finds, a value is being looked up in the cookie.Values dictionary and the result is being used as the replacement.

like image 120
ColinE Avatar answered Sep 22 '22 19:09

ColinE