SmartEnumerable and Regex.Matches

I wanted to use Jon Skeet SmartEnumerable to loop over Regex.Matches, but it doesn't work.

foreach (var entry in Regex.Matches("one :two", @"(?<!\w):(\w+)").AsSmartEnumerable())

Can someone explain to me why? And suggest a solution to make it work. Thank.

Error:

'System.Text.RegularExpressions.MatchCollection' does not contain a definition
for 'AsSmartEnumerable' and no extension method 'AsSmartEnumerable' accepting
a first argument of type 'System.Text.RegularExpressions.MatchCollection' could
be found (are you missing a using directive or an assembly reference?)
+3
source share
1 answer

EDIT: I think you forgot to insert the call .AsSmartEnumerable()into your sample code. The reason that will not compile is because the extension method only works on IEnumerable<T>, and not on, a non-generic interface IEnumerable.


, ; , entry object, MatchCollection IEnumerable<T> , IEnumerable.

, IEnumerable<T>, :

foreach (var entry in Regex.Matches("one :two", @"(?<!\w):(\w+)").Cast<Match>())
{ 
   ...
} 

, ( ):

foreach (Match entry in Regex.Matches("one :two", @"(?<!\w):(\w+)"))
{ 
   ...
} 

smart-enumerable - -:

var smartEnumerable = Regex.Matches("one :two", @"(?<!\w):(\w+)")
                           .Cast<Match>()
                           .AsSmartEnumerable();

foreach(var smartEntry in smartEnumerable)
{
   ...
}

using MiscUtil.Collections.Extensions; .

+7

Source: https://habr.com/ru/post/1773288/


All Articles