这个“Lambda Expression”是做什么的?
问题描述:
只是遇到下面的代码行,很难找到它的文档,它是一个lambda expression
?这是做什么的?这个“Lambda Expression”是做什么的?
temp = Regex.Replace(url, REGEX_COOKIE_REPLACE,match => cookie.Values[match.Groups["CookieVar"].Value]);
特别感兴趣的=>
。
答
如果你看一下更换文档,第三个参数是一个MatchEvaluator
:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx
这是需要一个Match
作为参数,并返回字符串替换它的委托。您的代码是使用lambda表达式限定MatchEvaluator
:
match => cookie.Values[match.Groups["CookieVar"].Value]
在此,对于每个匹配,该正则表达式发现,在值被在cookie.Values
词典查找并且结果被用作替换。
答
match => cookie.Values[match.Groups["CookieVar"].Value]
是一个捷径
delegate (Match match)
{
return cookie.Values[match.Groups["CookieVar"].Value];
}
答
的RegEx.Replace
为运行在url
REGEX_COOKIE_REPLACE
每场比赛的Lambda和“取代”了与之相匹配的lambda表达式的结果。
拉姆达(或简写代表)
match => cookie.Values[match.Groups["CookieVar"].Value]
使用“CookieVar”的Match,
的Group,
的Value
查找替换的cookie.Values
收藏。查找值取代了匹配。
要告诉你更多关于“CookieVar”组中,我们需要看到的REGEX_COOKIE_REPLACE.
它的[此重载(http://msdn.microsoft.com/en-us/library/ht1sxswy评估。 aspx) - 这是一个lambda,是的,指定[MatchEvaluator委托](http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx)。 – Rup 2011-05-17 09:21:24