I need to pass an array to a function in C# where the array may be a string[], int[], or double[] and may be 1 to multiple dimensions.
Reasoning:
I am needing to pass an array to a function to calculate its length, similar to:
public int getLength(array[] someArray, int dimension)
{
if(dimension > 0) //get length of dimension
return someArray.getLength(dimension);
else //get length of entire array
return someArray.Length;
}
Question:
How can I pass a generic array?
Or
Is there a better way to do this – say convert the array to a List?
For the purpose you cite, just pass
Array someArray– that should be fine, and covers any number of dimensions. For a vector (a 1-dimensional, 0-based array), you can use generics (GetLength<T>(T[] someArray)) – but that seems pointless when for a vector.Lengthis more convenient.But for an unknown number of dimensions (or for a non-vector single-dimensional array), you are limited to
Array.