Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and when to abandon the use of arrays in C#?

Tags:

I've always been told that adding an element to an array happens like this:

An empty copy of the array+1element is created and then the data from the original array is copied into it then the new data for the new element is then loaded

If this is true, then using an array within a scenario that requires a lot of element activity is contra-indicated due to memory and CPU utilization, correct?

If that is the case, shouldn't you try to avoid using an array as much as possible when you will be adding a lot of elements? Should you use iStringMap instead? If so, what happens if you need more than two dimensions AND need to add a lot of element additions. Do you just take the performance hit or is there something else that should be used?

like image 760
Keng Avatar asked Sep 16 '08 19:09

Keng


People also ask

When should we use array in C?

An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it.

When should I use an array instead of a list?

Arrays can store data very compactly and are more efficient for storing large amounts of data. Arrays are great for numerical operations; lists cannot directly handle math operations. For example, you can divide each element of an array by the same number with just one line of code.

Why are lists better than arrays?

The list is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. List occupies much more memory as every node defined the List has its own memory set whereas Arrays are memory-efficient data structure.

How do arrays end in C?

C arrays don't have an end marker. It is your responsibility as the programmer to keep track of the allocated size of the array to make sure you don't try to access element outside the allocated size. If you do access an element outside the allocated size, the result is undefined behaviour.


2 Answers

Look at the generic List<T> as a replacement for arrays. They support most of the same things arrays do, including allocating an initial storage size if you want.

like image 116
Joel Coehoorn Avatar answered Sep 30 '22 14:09

Joel Coehoorn


This really depends on what you mean by "add."

If you mean:

T[] array; int i; T value; ... if (i >= 0 && i <= array.Length)     array[i] = value; 

Then, no, this does not create a new array, and is in-fact the fastest way to alter any kind of IList in .NET.

If, however, you're using something like ArrayList, List, Collection, etc. then calling the "Add" method may create a new array -- but they are smart about it, they don't just resize by 1 element, they grow geometrically, so if you're adding lots of values only every once in a while will it have to allocate a new array. Even then, you can use the "Capacity" property to force it to grow before hand, if you know how many elements you're adding (list.Capacity += numberOfAddedElements)

like image 35
Alex Lyman Avatar answered Sep 30 '22 14:09

Alex Lyman