开发者

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

开发者 https://www.devze.com 2023-02-23 06:48 出处:网络
MatchEvaluator evaluator = (match) => { var splitPos = match.Value.IndexOf(\"=\\\"\"); var newValue = match.Value.Substring(0, splitPos + 2) +
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;
            开发者_开发知识库};

What does this code mean , I need to port this code which is in VS 2008 to VS 2005, the same is not available in VS 2005


c# 2.0 supports the delegate keyword, so it could be rewritten into this:

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;
};

And this is exactly the same as this:

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;
}

called with:

MatchEvaluator evaluator = OnEvaluator;

And what it is?

MSDN: Represents the method that is called each time a regular expression match is found during a Replace method operation.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消