有没有办法获取对 C# 数组元素的引用?

时间:2021-04-10 04:05:24

标签: c# arrays pointers ref

我想知道是否有办法在 c# 中获取对数组元素的引用。

类似这样但没有指针的东西:

SomeClass *arr[5];
...    
SomeClass** p = &arr[2];

到目前为止我得到了这个:

SomeClass arr[] = new SomeClass[5];
// Put something in the array
SomeClass p = arr[2]; // This will give me the initial object, not the array ref
p = new SomeClass(); // I want the array to now use this new object

1 个答案:

答案 0 :(得分:2)

这不是你想要的吗?

var arr = new SomeClass[5];
ref SomeClass p = ref arr[2];
p = new SomeClass();
相关问题