2011-05-17 124 views
8

只是遇到下面的代码行,很难找到它的文档,它是一个lambda expression?这是做什么的?这个“Lambda Expression”是做什么的?

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

特别感兴趣的=>

+0

它的[此重载(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

回答

9

如果你看一下更换文档,第三个参数是一个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词典查找并且结果被用作替换。

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

是一个捷径

delegate (Match match) 
{ 
    return cookie.Values[match.Groups["CookieVar"].Value]; 
} 
1

RegEx.Replace为运行在urlREGEX_COOKIE_REPLACE每场比赛的Lambda和“取代”了与之相匹配的lambda表达式的结果。

拉姆达(或简写代表)

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

使用“CookieVar”的Match,Group,Value查找替换的cookie.Values收藏。查找值取代了匹配。

要告诉你更多关于“CookieVar”组中,我们需要看到的REGEX_COOKIE_REPLACE.