I do not understand why var m does not return Match. I havent checked, but it seems to return an object.
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
MatchCollectionimplements IEnumerable, but not IEnumerable<Match>, therefore, the compiler can infer the type of the variable as object, rather than Match.
MatchCollection
IEnumerable
IEnumerable<Match>
object
Match
Regex.Matchesreturns MatchCollectionwhich implements IEnumerable, not IEnumerable<Match>.
Regex.Matches
Therefore, the default element type is an object. When using an element type Matchin foreach, you get the expected element type.
var , . Matches MatchCollection, IEnumerable, IEnumerable<Match>. Current, , IEnumerable.GetEnumerator(), object. , var m object, Match.
var
Matches
Current
IEnumerable.GetEnumerator()
m
foreach, , cast, . , InvalidCastException .
foreach
InvalidCastException
Source: https://habr.com/ru/post/1723318/More articles:Trying to install Ruby on Rails on OSX and I get Gem :: RemoteSourceException - ruby-on-railsКак определить ОС Win7 Home Premium, Win7 Professional, Win7 Enterprise или Win7? - apiWhy is http response in chunked transfer-encoding only for some clients - asp.net-mvcSpring in a GWT project? - javaWhats Algorithm or code to get the ordinal position of an element in a list sorted by value in C ++ - c ++Flex + PHP: Flash Builder 4 vs Eclipse - eclipseWhat is QT or C ++ open source for ordinary sorting - c ++Guidelines / principles for designing packages and components - packageHow to get row index of selected cell in WPF grid - wpfHow can I force form / control validation in Compact Framework? - .netAll Articles