Solved

Passing fixed size array as parameter, ensuring array is proper size

Posted on 2011-09-10
3
279 Views
Last Modified: 2012-05-12
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.)
0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
Irzana earned 500 total points
ID: 36518050
You cannot define the array size for method parameter, inside the method you can check for array size before doing any logic.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36519077
__NO POINTS__

Agreed, check inside the callee.
0
 

Author Closing Comment

by:deleyd
ID: 36520239
But that makes it a runtime error. I was hoping to catch it at compile time.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question