deleyd
asked on
Passing fixed size array as parameter, ensuring array is proper size
Is there a way to specify the array size when an array is a parameter to a method? e.g.
static void MyMethod(int[] array)
{
//Array must be exactly 3 elements in size.
}
<example>
int[] array = { -5, -6, -7 };
MyMethod(array); //OK
int[] array2 = { -5, -6, -7, -8 };
MyMethod(array2); //compiler error
or do I have to construct an entire class consisting of one 3 element int array just so I can ensure the array is the correct size? (I'm guessing I have to go with the class.)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
But that makes it a runtime error. I was hoping to catch it at compile time.
Agreed, check inside the callee.