MyJaggedIntArr(ICollection<ICollection<int>> items) rejects List<List<Int>>


P

per9000

Hi,

*background*
I want a class containing an int[][] (a list of sets of integer). This
should be hidden for the user and he/she should be able to insert
his/her favourite data structure so to be a little fancy I thought I'd
do a Ctor like this:

// this is the "core": private int[][] sets;

public MyJaggedIntArr(ICollection<ICollection<int>> items)
{
this.sets = new int[items.Count][];
int c = 0;
foreach (ICollection<int> set in items)
{
sets[c] = new int[set.Count];
set.CopyTo(sets[c++], 0);
}
}

*the problem*
This works fine if I insert an int[][] into the Ctor, but I was unable
to pass a List<List<Int>> I got the following exception:

Argument '1': cannot convert from
'System.Collections.Generic.List<System.Collections.Generic.List<int>>'
to
'System.Collections.Generic.ICollection<System.Collections.Generic.ICollection<int>>'

*the question*
Is there some even more basic interface than ICollection that includes
lists and ICollection that I could use? Or should I just make four
constructors?

1 - List<List<Int>>
2 - List<ICollection<Int>>
3 - ICollection<List<Int>>
4 - ICollection<ICollection<Int>>

I also might need to add a fifth one
(5 - int[,])

Or should I just force the user to insert int[][]?

thanks

/Per
 
Ad

Advertisements

G

gary

Hello,

If the users will always be adding int[][] then I would force that. Do
they really need the ability to add generic lists of ints?
 
Ad

Advertisements

P

per9000

If the users will always be adding int[][] then I would force that. Do
they really need the ability to add generic lists of ints?
Hi Gary,
since I don't know how users will store their int's I'd be happy to
find something generic. Also int[][] is an implementation of
ICollection<ICollection<Int>> so the Ctor above can deal with it.

But on the other hand: do they really need a very generic constructor?
No, probably not.

Also I would like the user to not know/care about how I store the int's
since they might get funny ideas (I know I get them all the time).

There is really little trouble for me to write N^2 Constructors. But it
really intrigued me that List<T> was *not* an implementation of
ICollection<T> so it is also a philosophical/structural question.

/Per
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top