【发布时间】:2021-07-05 21:35:37
【问题描述】:
我想知道是否有办法在 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
【问题讨论】:
-
如果
SomeClass是一个引用类型,那么你在arr[2]有一个对对象的引用,并且改变p的属性将改变arr[2]中的相同属性。跨度> -
我将编辑问题以更好地解释问题
-
没有指针就无法做到这一点
-
为什么要将此字段设为字段?听起来像一个XY问题。如果您只是想要一种“获取和设置
SomeClass”的方法,则可以存储Func<SomeClass>和Action<SomeClass>。