Solved

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

Posted on 2011-09-10
3
273 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
3 Comments
 
LVL 10

Accepted Solution

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

Expert Comment

by:käµfm³d 👽
Comment Utility
__NO POINTS__

Agreed, check inside the callee.
0
 

Author Closing Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
reading json from C# REST service 1 49
Need help with a query 6 53
C# Export DataGridView 4 37
Different Delete Messages 7 4
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now