我注意到以下代码中似乎有些奇怪:
MatchCollection mc = Regex.Matches(myString, myPattern);
foreach(var match in mc)
Console.WriteLine(match.Captures[0]); // <-- this line is invalid, unless I replace 'var' above with 'Match'
变量match
的类型为Object
而不是Match
。我通常使用var
来枚举集合,没有出现这样的问题。为什么MatchCollection
会有所不同?
IEnumerable<Match>
这个简单的步骤。 - Jamiec