Reputation: 1524
Is it possible to create a list or array of pointers in C#?
I want to have a list of T*
rather than use IntPtr
because i am forever having to type Marshal Ptr To Structure
methods all over the place rather than directly access methods via ->
command.
I tried making a list with T* but it says it cannot use it as a type. So is my only option to just constantly convert when needed from the IntPtr
?
Upvotes: 1
Views: 367
Reputation: 27254
You can declare an array of pointer.
unsafe Vector3*[] vectorPointerArray;
You cannot use pointer as a generic type parameter like List<Vector3*>
, this is still under consideration.
Because of the feasibility 1, you can create a collection to hold pointers, though it cannot implement generic interfaces like IList<T>
.
unsafe class UnsafePointerList<T> where T : unmanaged
{
private T*[] _items;
public T* this[int index] => _items[index];
}
UnsafePointerList<Vector3> vectorPointerList;
var x = vectorPointerList[0]->x;
Upvotes: 2