3

as I am implementing the ICollection-Interface in my class I want to implement the CopyTo-Method and I have to throw an Argument-exception if the array is multidimensional. What is meant by this? The head of my method is this

public void CopyTo(MyClass[] array, int arrayIndex)

I thought these brackets would mean that the given array is one-dimensional but when I automatically import the comments from the Interface the comment appears, that I have to check for multidimensionality. Can someone explain it to me?

With kind regards

Sebastia

Sebastian Müller
  • 5,471
  • 13
  • 51
  • 79

1 Answers1

3

You can look at Array.Rank? However, T[] is one dimensional. You mainly need to check the rank when all you know is "Array". This is because the actual method is exposed via Array, not T[].

So in short - don't worry about it in this scenario ;-p

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • The interface signature for ICollection is actually CopyTo(T[]array,int arrayIndex) and the ICollection comments tells you to check for multidimension arrays so this must mean that T[] is not a constraint for single dimension arrays. Are you sure about this? – terjetyl Dec 09 '08 at 10:22
  • Yes. Specifically, you "cannot convert from 'int[*,*]' to 'int[]'. Try: `int[,] test = { { 1, 2 }, { 3, 4 } };` `ICollection list = new List() { 1, 2 };` `list.CopyTo(test, 0);` – Donal Lafferty Mar 08 '12 at 16:26