I have a web service that has an array as a parameter. The problem is I only want to let the user send me an array of 15 entries. Of course if they sent me a bigger array I could write code to only take the first 15, but I dont want to do that. I would like to let the user understand we need 15 and only 15 entries and no more.
The only way I know to initialize an array to a fixed value is instantiate it and I cannot do that as a parameter.
Maybe an array as a parameter isnt the best option? Any Ideas?
Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.
A fixed array is an array for which the size or length is determined when the array is created and/or allocated. A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed.
So, a[0] is the element in address 1000 , a[1] is the element in the address 1001 , …, a[57] is the element in the address 1057 , and so on… Definitely, a[i] means element in the address 1000 + i . So, a[-1] means the element in the address 1000 + -1 , which is element in the address 999 .
Arrays are fixed size. Once we initialize the array with some int value as its size, it can't change.
You can't really control the array length of the input to your operation, but you can add some dynamic validation (i.e., throw an ArgumentException if a user passes an array of a different length). Another option would be to simply have 15 parameters to your operation, but that may not be something too readable if the parameters are related and an array would make more sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With