2

Like in PHP and some other languages, is there a way to add a value to an array without specifying the index?

int[] aWhich = {};

aWhich[] = 1;

Thanks.

7
  • If you want features like that, why even use an array instead of a comfy collection, like List<int>? Commented Jul 3, 2013 at 15:10
  • See stackoverflow.com/questions/594853/dynamic-array-in-c-sharp Commented Jul 3, 2013 at 15:10
  • @JanDoerrenhaus You should never be using an ArrayList outside of old legacy apps. You should be using List. Commented Jul 3, 2013 at 15:11
  • If you could describe your scenario a little more, maybe we could give you a good anwser. Like this, the answer is just no. Commented Jul 3, 2013 at 15:12
  • 1
    What do you expect that code to do Gabriel. Do you want it to add it to the end of the collection, the start, do you want an unordered set of items in this collection, or what? Commented Jul 3, 2013 at 15:12

5 Answers 5

9

Not to an Array or any other type since the indexer operator must have at least one parameter (through it does not have to be an int).

You can add to the end of a List, though:

List<int> aWhich = new List<int>();

aWhich.Add(1);
Sign up to request clarification or add additional context in comments.

Comments

1

First of all you have to specify the maximum number of values that the array can hold:

int[] MyArray = new int[14];

Here 14 is the maximum number of values that MyArray can hold.

int value = 0;
void MyFuntion(){
       MyArray[value] = 1;
       value++;
}

In this way you can add values without specifying index number it will automatically put the index.

Comments

0

You can't. Arrays in C# (and .NET) are immutable (in terms of their size, not necessarely their content) and you access their values by the index. What's you are looking for is a List, ArrayList or something that might suits your need better in the System.Collections or System.Collections.Generic namespaces.

3 Comments

Nobody is looking for an ArrayList, ever.
@user7116 I agree, but it's still an option. Easier to understand than genericity from a beginner perspective.
We call that, "setting them up for failure."
0

There is one other way, Fist add elements to a List, then convert it into array.

Eg:

var intList = new List<int>();
intList.Add(1);
intList.Add(2);

var intArray = intList.ToArray();

Comments

0

Edit: this method does NOT work for adding a new array item.

myArray[myArray.Length] = newArrayItem;

.Length - # will work for overwriting array items.

4 Comments

this can't work. If you specify array of 5 elements then it will try to add new item to the 5th place and you will get exception.
@knil4_crack I'm by no means an experienced C# programmer, but I'm fairly sure I used this technique successfully in a previous project. Although I don't remember in what context. Maybe it works, just not in the way I described? Or I'm just wrong.
is this what you mean? Give it a shot dotnetfiddle.net/zAJcm0
@knil4_crack I can't find the use of Length in my most recent C# project. I might have switched to using a list instead of an array, I can't remember why, but it seems you're right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.