Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

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

Open in new window

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
Avatar of Irzana
Irzana
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kaufmed
__NO POINTS__

Agreed, check inside the callee.
Avatar of deleyd

ASKER

But that makes it a runtime error. I was hoping to catch it at compile time.