C # 等价于 C + + 向量,具有连续内存?

C + + 向量的 C # 等价物是什么?

我正在搜索这个特性:

拥有一个连续存储内存的动态数组,与标准数组相比,该数组的访问没有性能损失。

我正在搜索,他们说 .NET equivalent to the vector in C++ is the ArrayList,所以:

ArrayList 具有连续内存特性吗?

203343 次浏览

You could use a List<T> and when T is a value type it will be allocated in contiguous memory which would not be the case if T is a reference type.

Example:

List<int> integers = new List<int>();
integers.Add(1);
integers.Add(4);
integers.Add(7);


int someElement = integers[1];

use List<T>. Internally it uses arrays and arrays do use contiguous memory.

C# has a lot of reference types. Even if a container stores the references contiguously, the objects themselves may be scattered through the heap

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.