繁体   English   中英

如何在C#2.0中重写lambda表达式?

[英]How do I rewrite a lambda expression in C# 2.0?

MatchEvaluator evaluator = (match) =>
            {
                var splitPos = match.Value.IndexOf("=\"");
                var newValue = match.Value.Substring(0, splitPos + 2) +
                    "RetrieveBuildFile.aspx?file=" +
                    prefix +
                    match.Value.Substring(splitPos + 2);
                return newValue;
            };

这段代码意味着什么,我需要将VS 2008中的这段代码移植到VS 2005,VS 2005中也没有

c#2.0支持delegate关键字,因此可以将其重写为:

MatchEvaluator evaluator = delegate(Match match) {
    int splitPos = match.Value.IndexOf("=\"");
    string newValue = match.Value.Substring(0, splitPos + 2) +
                        "RetrieveBuildFile.aspx?file=" +
                        prefix +
                        match.Value.Substring(splitPos + 2);
    return newValue;
};

这与此完全相同:

static string OnEvaluator(Match match) {
   int splitPos = match.Value.IndexOf("=\"");
   string newValue = match.Value.Substring(0, splitPos + 2) + 
       "RetrieveBuildFile.aspx?file=" + 
       prefix + 
       match.Value.Substring(splitPos + 2);
   return newValue;
}

叫来:

MatchEvaluator evaluator = OnEvaluator;

它是什么?

MSDN:表示每次在Replace方法操作期间找到正则表达式匹配时调用的方法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM