1

I spent quite some time solving this but I cannot figure it out. I have the following Java method signature:

public static <T> boolean isSameCollectionSets(
    Collection<? extends Collection<T>> set1, 
    Collection<? extends Collection<T>> set2)

This method takes two collections of collections of type T. And it works in Java. Also, the compiler is able to get the type T himself when passing correct arguments.

Now I want to port this exact same method to C#. So far, I have this:

public static bool IsSameCollectionSets<T>(ICollection<T> set1, ICollection<T> set2) 
    where T : ICollection<T>

However, this fails when I try to do the following in C#:

ICollection<List<int>> collection1 = new List<List<int>>();
ICollection<HashSet<int>> collection2 = new HashSet<HashSet<int>>();

var result = IsSameCollectionSets(collection1, collection2);

The IsSameCollectionSets method is curly underlined and the compiler says:

"The type arguments for method 'bool IsSameCollectionSets(ICollection, ICollection)' cannot be inferred from the usage. Try specifying the type arguments explicitly."

Now, I tried to give the type arguments explicitly, but nothing has worked so far.

Where is my error? Many thanks in advance!

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Biocoder
  • 65
  • 9
  • You are essentially trying to do this `ICollection> collection1 = new List>()` which doesn't work in C#. – vcsjones Nov 10 '14 at 17:58
  • The problem is because you are using `T` as a single type. The first parameter, `T` is `List`, in the second, it's `HashSet`. Modify your method to be: `public static bool IsSameCollectionSets(ICollection set1, ICollection set2) where TOne : ICollection, TTwo : ICollection` – Rob Nov 10 '14 at 17:59
  • @Rob not sure how that is helpful. That doesn't even compile for one, and two the OP is trying to ensure that the elements of the collection's collection are the same, and you're allowing them to be two different types. – vcsjones Nov 10 '14 at 18:01
  • @vcsjones Oops, I misread the example, I didn't see the wrapping of ICollection<> – Rob Nov 10 '14 at 18:04
  • 1
    That being said, going by the name of the method, you don't need to use the ICollection<> interface, as a comparison is readonly. `public static bool IsSameCollectionSets(IEnumerable> set1, IEnumerable> set2) ` Will work in this situation. – Rob Nov 10 '14 at 18:06
  • @Rob Wow, thank you for this very quick response. I got it working by using `IEnumerable` instead of `ICollection`. I'm new to SO and not sure about how to mark your post as the answer... – Biocoder Nov 10 '14 at 18:26

1 Answers1

0

I found a working solution thanks to @Rob.

Instead of using ICollection<T> I use IEnumerable<T>:

public static bool IsSameCollectionSets<T>(IEnumerable<IEnumerable<T>> set1,
IEnumerable<IEnumerable<T>> set2)

Also, in comparison to the code in the question, I use interfaces for the instantiation:

ICollection<IList<int>> collection1 = new List<IList<int>>();
ICollection<ISet<int>> collection2 = new HashSet<ISet<int>>();
Biocoder
  • 65
  • 9