1

I have this method:

public static HashSet<myclass> mymethod(int somenum)
{
    myclass[,] myarray = new myclass[somenum, somenum];                
    // process myarray here
    return new HashSet<myclass>(myarray);
}

but on the last line, I get two errors:

Error 1 The best overloaded method match for 'System.Collections.Generic.HashSet.HashSet(System.Collections.Generic.IEnumerable)' has some invalid arguments

Error 2 Argument 1: cannot convert from 'myclass[,]' to 'System.Collections.Generic.IEnumerable'

but according to MSDN, arrays implement IEnumerable and IEnumerable<T>. So why am I getting this error, and how can I fix it?

Community
  • 1
  • 1
resgh
  • 974
  • 2
  • 12
  • 22
  • 2
    What are you expecting it to do? A `HashSet` is one-dimensional. – Ry- Mar 31 '12 at 04:12
  • 3
    possible duplicate of [Why does C# Multidimensional arrays not implement IEnumerable?](http://stackoverflow.com/questions/275073/why-does-c-sharp-multidimensional-arrays-not-implement-ienumerablet) – Alexei Levenkov Mar 31 '12 at 04:12
  • 1
    alright... thanks anyway – resgh Mar 31 '12 at 05:57
  • @minitech kindly post it as an answer so that it helps future visitors.. – nawfal Mar 31 '12 at 08:47
  • @testgo post the answer as an answer and accept it. Visit this http://meta.stackexchange.com/questions/54718/how-to-handle-questions-which-are-answered-in-the-comments – nawfal Mar 31 '12 at 08:48
  • ??? i posted an answer but it got converted to a comment. "trivial answer converted to comment" it says. howto fix this? – resgh Mar 31 '12 at 13:14
  • @minitech I just meant if you could answer it rather than a comment, then it would help future visitors.. – nawfal Mar 31 '12 at 13:53
  • @nawfal: No, asking "What are you expecting to do" is not an answer. – Ry- Mar 31 '12 at 13:54
  • @minitech The class `HashSet<>` has a public constructor that takes a generic `IEnumerable<>` as its sole argument. Unfortunately, the multi-dimensional array `myarray` is only a non-generic `IEnumerable`. To fix the error, using Linq, one can say `new HashSet(myarray.Cast())`. – Jeppe Stig Nielsen Jun 11 '12 at 12:27
  • I posted a "Community Content" comment on the MSDN page linked in the question, to correct their error. My comment will probably appear soon. But it's stil a shame that a `T[,]` is not an `IEnumerable`. – Jeppe Stig Nielsen Jun 11 '12 at 12:31

0 Answers0