Solved

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

Posted on 2011-09-10
3
278 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Linq - Join two objects into one 3 55
Converting PHP triple des decrypt to c# 9 170
Need help with a query 14 54
Put File 6 37
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Suggested Courses

738 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