我不明白为什么 var m 不返回 Match。我还没有检查,但它似乎正在返回一个对象。

        foreach (var m in Regex.Matches("dummy text", "(mm)"))
            var sz = m.Groups[1]; // error CS1061: 'object' does not contain a definition for 'Groups' and no extension method 'Groups' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

        foreach (Match m in Regex.Matches("dummy text", "(mm)"))
            var sz = m.Groups[1]; //ok

最佳答案

MatchCollection 实现 IEnumerable 而不是 IEnumerable<Match> 因此编译器只能将变量的类型推断为 object 而不是 Match

关于c# - 区别 var 和 Type。为什么 var 在这里不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1761866/

10-12 07:40
查看更多