C
C#•2mo ago
ReK

A .Select() that I believe should work but it doesn't.

As title says, this is the script (not my real script just one made to show the issue):
using System;
using System.Text.RegularExpressions;
using System.Linq;


namespace test
{
class Program
{
static void Main()
{
MatchCollection matchCollection = Regex.Matches("", "");
matchCollection.Select(n => n); // works
GroupCollection groupCollection = matchCollection[1].Groups;
groupCollection.Select(n => n); // Doesn't work
}
}
}
using System;
using System.Text.RegularExpressions;
using System.Linq;


namespace test
{
class Program
{
static void Main()
{
MatchCollection matchCollection = Regex.Matches("", "");
matchCollection.Select(n => n); // works
GroupCollection groupCollection = matchCollection[1].Groups;
groupCollection.Select(n => n); // Doesn't work
}
}
}
With <GroupCollection> it says
'GroupCollection' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'GroupCollection' could be found (are you missing a using directive or an assembly reference?)
'GroupCollection' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'GroupCollection' could be found (are you missing a using directive or an assembly reference?)
But it works with <MatchCollection>, and I'm using .NET 8.0 so it isn't an issue about it not having generic interfaces or whatever, why is this happening when both have IEnumerable<T>. This is their declarations
public class MatchCollection : ICollection<Match>, IEnumerable<Match>, IEnumerable, IList<Match>, IReadOnlyCollection<Match>, IReadOnlyList<Match>, ICollection, IList
public class MatchCollection : ICollection<Match>, IEnumerable<Match>, IEnumerable, IList<Match>, IReadOnlyCollection<Match>, IReadOnlyList<Match>, ICollection, IList
and
public class GroupCollection : ICollection<Group>, IEnumerable<Group>, IEnumerable, IEnumerable<KeyValuePair<string, Group>>, IList<Group>, IReadOnlyCollection<KeyValuePair<string, Group>>, IReadOnlyCollection<Group>, IReadOnlyDictionary<string, Group>, IReadOnlyList<Group>, ICollection, IList
public class GroupCollection : ICollection<Group>, IEnumerable<Group>, IEnumerable, IEnumerable<KeyValuePair<string, Group>>, IList<Group>, IReadOnlyCollection<KeyValuePair<string, Group>>, IReadOnlyCollection<Group>, IReadOnlyDictionary<string, Group>, IReadOnlyList<Group>, ICollection, IList
3 Replies
ReK
ReKOP•2mo ago
And yes I know I can do
<GroupCollection>.Cast<Group>().Select(...)
<GroupCollection>.Cast<Group>().Select(...)
But I really wanted to directly do <GroupCollection>.Select(), but even if it's not possible I want to understand why it isn't, to me it seems like it should work.
Tvde1
Tvde1•2mo ago
this is because GroupCollection implements multiple IEnumerables. The compiler won't know which you want to use the .Select() on. You can do:
groupCollection.Select<Group, Group>(n => n);
groupCollection.Select<Group, Group>(n => n);
Where now it knows to use the
public class GroupCollection :
...,
System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Group>,
public class GroupCollection :
...,
System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Group>,
mtreit
mtreit•2mo ago
It implements multiple IEnumerables? 🤔 I see that indeed it does. That's...different.

Did you find this page helpful?