![](/img/trans.png)
[英]Why does Resharper suggest a code change and then complain about the change?
[英]Why does C# not complain about compiling this line of code
string json = "{ "Name": "Tom" }";
var regex = new Regex(@"\\x([a-fA-F0-9]{2})");
json = regex.Replace(json, match => char.ConvertFromUtf32(Int32.Parse(match.Groups[1].Value, System.Globalization.NumberStyles.HexNumber)));
我的代码中未定义变量“ match”。 代码执行没有问题,但是我想知道为什么C#不会抱怨它是未定义的? 在此代码之后,如果我写:
x = 1;
C#会抱怨未定义x。 这是怎么回事?
在这里, match
声明了参数-因此match
在lambda内定义得很好。 本质上(因为它正在编译为委托),对于类似以下内容,这都是语法糖:
static string HazNoName(Match match)
{
return char.ConvertFromUtf32(Int32.Parse(match.Groups[1].Value,
System.Globalization.NumberStyles.HexNumber));
}
//...
json = regex.Replace(json, new MatchEvaluator(HazNoName));
(尽管在这种情况下,实际上C#编译器将缓存并重新使用委托实例,因为没有捕获的上下文)
match
是lambda表达式中的一个参数,为什么编译器会抱怨?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.