What's the C# equivalent of C++ vector?
I am searching for this feature:
To have a dynamic array of contiguously stored memory that has no performance penalty for access vs. standard arrays.
I was searching and they say .NET equivalent to the vector in C++ is the ArrayList
, so:
Do ArrayList have that contiguous memory feature?
It looks like CLR / C# might be getting better support for Vector<> soon.
use List<T>
. Internally it uses arrays and arrays do use contiguous memory.
First of all, stay away from Arraylist
or Hashtable
. Those classes are to be considered deprecated, in favor of generics. They are still in the language for legacy purposes.
Now, what you are looking for is the List<T>
class. Note that if T is a value type you will have contiguos memory, but not if T is a reference type, for obvious reasons.
C# has a lot of reference types. Even if a container stores the references contiguously, the objects themselves may be scattered through the heap
Source: Stackoverflow.com