[英]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
? 刚刚遇到以下代码行并且很难找到它的文档,它是一个lambda expression
吗? What does this do? 这是做什么的?
temp = Regex.Replace(url, REGEX_COOKIE_REPLACE,match => cookie.Values[match.Groups["CookieVar"].Value]);
Specifically interested in the =>
. 特别感兴趣的是=>
。
If you look at the documentation for Replace, the 3rd argument is a MatchEvaluator
: 如果查看Replace的文档,第3个参数是MatchEvaluator
:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx 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. 这是一个委托,它将Match
作为参数并返回字符串以替换它。 Your code is defining a MatchEvaluator
using a lambda expression: 您的代码使用lambda表达式定义MatchEvaluator
:
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. 在这里,对于Regex找到的每个匹配,在cookie.Values
字典中查找一个值,结果用作替换。
match => cookie.Values[match.Groups["CookieVar"].Value]
is a shortcut to 是一个快捷方式
delegate (Match match)
{
return cookie.Values[match.Groups["CookieVar"].Value];
}
The RegEx.Replace
runs the lambda for every match of REGEX_COOKIE_REPLACE
in url
and "replaces" the match with the lambdas result. RegEx.Replace
为url
中的REGEX_COOKIE_REPLACE
每个匹配运行lambda,并用lambdas结果“替换”匹配。
The lambda (or shorthand delegate) lambda(或速记代表)
match => cookie.Values[match.Groups["CookieVar"].Value]
uses the Value
of the "CookieVar" Group,
of the Match,
to look up a substitution in the cookie.Values
collection. 使用Match,
的“CookieVar” Group,
的Value
来查找cookie.Values
集合中的cookie.Values
。 The lookup value replaces the match. 查找值替换匹配。
To tell you more about the "CookieVar" group we would need to see an evaluation of REGEX_COOKIE_REPLACE.
为了告诉您有关“CookieVar”组的更多信息,我们需要查看REGEX_COOKIE_REPLACE.
的评估REGEX_COOKIE_REPLACE.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.