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.

Gabriel Nahmias
  • 920
  • 3
  • 15
  • 20
  • If you want features like that, why even use an array instead of a comfy collection, like `List`? – Jan Dörrenhaus Jul 03 '13 at 15:10
  • See http://stackoverflow.com/questions/594853/dynamic-array-in-c-sharp – RoadieRich Jul 03 '13 at 15:10
  • @JanDoerrenhaus You should never be using an `ArrayList` outside of old legacy apps. You should be using `List`. – Servy Jul 03 '13 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. – studert Jul 03 '13 at 15:12
  • @Servy You are right, of course. Got my languages mixed up again. – Jan Dörrenhaus Jul 03 '13 at 15:12
  • Please don't use ArrayList use List instead. You'll have an overhead from boxing if you use an ArrayList with any value types such as your `int`s. – Doctor Jones Jul 03 '13 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? – Servy Jul 03 '13 at 15:12

5 Answers5

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);
D Stanley
  • 149,601
  • 11
  • 178
  • 240
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.

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.

Simon Belanger
  • 14,752
  • 3
  • 41
  • 35
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();
Akbar Badhusha
  • 2,415
  • 18
  • 30
0

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

myArray[myArray.Length] = newArrayItem;

.Length - # will work for overwriting array items.

theRPGmaster
  • 107
  • 1
  • 6
  • 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. – knile Dec 28 '19 at 06:49
  • @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. – theRPGmaster Dec 30 '19 at 01:14
  • is this what you mean? Give it a shot https://dotnetfiddle.net/zAJcm0 – knile Jan 02 '20 at 11:04
  • @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. – theRPGmaster Jan 03 '20 at 23:06