C# equivalent of C++ vector, with contiguous memory?

C#C++Vector

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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];

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

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

Solution 5 - C#

It looks like CLR / C# might be getting better support for Vector<> soon.

http://blogs.msdn.com/b/dotnet/archive/2014/04/07/the-jit-finally-proposed-jit-and-simd-are-getting-married.aspx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionedgarmtzeView Question on Stackoverflow
Solution 1 - C#Darin DimitrovView Answer on Stackoverflow
Solution 2 - C#Bala RView Answer on Stackoverflow
Solution 3 - C#Matteo MoscaView Answer on Stackoverflow
Solution 4 - C#Armen TsirunyanView Answer on Stackoverflow
Solution 5 - C#rjdevereuxView Answer on Stackoverflow