S
S
Sergey Konovalov2015-10-14 14:27:23
.NET
Sergey Konovalov, 2015-10-14 14:27:23

What is the difference between List ArrayList and Array?

What is the difference and in what cases, which is better to use?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mrrl, 2015-10-14
@zakar1ya

The size of an Array is set when it is created and must be carefully monitored. Accessing it is slightly faster than accessing a List. You can get a pointer to an element via fixed. If there are structures in Array, you can change their fields: A[x].B=c;
An array element can be passed to a function as ref or out.
The advantage of a List is that its size changes automatically when an element is added. It is impossible to change the fields of structures in it, it is also impossible to pass elements by reference.
ArrayList is not a generic type (it came from the first version of C#), so it consists only of objects. With the advent of List, it became not very necessary, except for cases when you really need to store objects of different types - but even then List<object> will completely replace it.

D
Dmitry Kovalsky, 2015-10-14
@dmitryKovalskiy

With such questions, it is customary to go to MSDN and RSDN. There are a lot of differences, but only experience will tell you when to use ... well, or Senior.
If on key points. - an array is created by the syntax like int[] a = new int[count].
Collections are created differently. For an array, as a rule, it is logically justified to refer to the index, For List, iteration of everything (foreach) or selection of elements by features through LINQ is more often used. Why ArrayList is needed, I don't know. It has one key drawback - it only stores object. This means that you can add objects of any type there (the List itself is strongly typed at the creation stage), but it also means that you need to at least roughly imagine what is stored there and how, and also use A LOT of type conversions. Let my colleagues add some more points to me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question