将数组的位置存储在c#中

时间:2021-12-04 18:07:34

I want to be able to store a reference to an array, but not really sure if pointers are necessary or where to start. I'm looking for functionality similar to using ref with a function, but in a variable.

我希望能够存储对数组的引用,但不确定是否需要指针或从何处开始。我寻找的功能类似于在函数中使用ref,但在变量中。

2 个解决方案

#1


4  

Suppose you have an array:

假设你有一个数组:

int[] a = new int[42];

The variable a is a reference to the array. You can make another one like this:

变量a是对数组的引用。你可以再做一个这样的:

int[] b = a;

Now a and b refer to the same array.

现在a和b指向相同的数组。

You can assign a value to an element like this:

您可以为这样的元素分配值:

a[0] = 666;

And then see that same value using the other reference:

然后用另一个参考文献来看同样的值:

Debug.Assert(b[0] == 666);

What this boils down to is the fact that arrays are reference types. So the assignment operator copies the reference to the object, and does not copy the object itself. Contrast this with value types, e.g. structs, for which the assignment operator copies the value of the object.

这可以归结为数组是引用类型这一事实。因此赋值操作符将引用复制到对象,而不复制对象本身。与赋值操作符复制对象的值的值类型(例如struct)相比。

#2


0  

A pointer (a reference to a memory location), in a high level language such as C#, is not necessary, if you want to store the reference to an array, because they're reference types. All reference type variables work like pointers, but the process of referencing to a memory location, in high level languages such as C#, is totally automatic and it is hidden to the user.

如果您希望存储对数组的引用,则不需要使用c#等高级语言中的指针(对内存位置的引用),因为它们是引用类型。所有引用类型变量都像指针一样工作,但是引用内存位置的过程,在高级语言(如c#)中,是完全自动的,并且对用户是隐藏的。

I have answered a question about pointers (in Java) here. Even if the language is different, Java is a high level language like C# and the process of referencing to a memory location is similar.

我已经在这里回答了一个关于指针(Java)的问题。即使语言不同,Java也是一种高级语言,比如c#,引用内存位置的过程也是类似的。

About your question, if you have an array like this:

关于你的问题,如果你有这样一个数组:

int[] myArray = new int[2];

you can store its reference in another array like this:

您可以将它的引用存储在另一个数组中,如下所示:

int[] myStoredArrayRef = myArray;

Now, the two objects are referring to the same array, because they're referring to the same 'location' in memory (you copied its reference). Indeed, if you modify an element of myArray in this way:

现在,这两个对象引用的是同一个数组,因为它们引用的是内存中相同的“位置”(您复制了它的引用)。事实上,如果你用这种方式修改myArray的一个元素:

myArray[0] = 123;

also the element of myStoredArrayRef will change:

myStoredArrayRef元素也会改变:

Console.WriteLine(myStoredArrayRef[0]); //it will print '123';

You're able also to store a reference to a List, because they're reference types (as rightly pointed out by @DavidHeffernan) like the arrays, with the same procedure.

您还可以存储对列表的引用,因为它们是引用类型(如@DavidHeffernan所正确指出的),类似于数组,使用相同的过程。

#1


4  

Suppose you have an array:

假设你有一个数组:

int[] a = new int[42];

The variable a is a reference to the array. You can make another one like this:

变量a是对数组的引用。你可以再做一个这样的:

int[] b = a;

Now a and b refer to the same array.

现在a和b指向相同的数组。

You can assign a value to an element like this:

您可以为这样的元素分配值:

a[0] = 666;

And then see that same value using the other reference:

然后用另一个参考文献来看同样的值:

Debug.Assert(b[0] == 666);

What this boils down to is the fact that arrays are reference types. So the assignment operator copies the reference to the object, and does not copy the object itself. Contrast this with value types, e.g. structs, for which the assignment operator copies the value of the object.

这可以归结为数组是引用类型这一事实。因此赋值操作符将引用复制到对象,而不复制对象本身。与赋值操作符复制对象的值的值类型(例如struct)相比。

#2


0  

A pointer (a reference to a memory location), in a high level language such as C#, is not necessary, if you want to store the reference to an array, because they're reference types. All reference type variables work like pointers, but the process of referencing to a memory location, in high level languages such as C#, is totally automatic and it is hidden to the user.

如果您希望存储对数组的引用,则不需要使用c#等高级语言中的指针(对内存位置的引用),因为它们是引用类型。所有引用类型变量都像指针一样工作,但是引用内存位置的过程,在高级语言(如c#)中,是完全自动的,并且对用户是隐藏的。

I have answered a question about pointers (in Java) here. Even if the language is different, Java is a high level language like C# and the process of referencing to a memory location is similar.

我已经在这里回答了一个关于指针(Java)的问题。即使语言不同,Java也是一种高级语言,比如c#,引用内存位置的过程也是类似的。

About your question, if you have an array like this:

关于你的问题,如果你有这样一个数组:

int[] myArray = new int[2];

you can store its reference in another array like this:

您可以将它的引用存储在另一个数组中,如下所示:

int[] myStoredArrayRef = myArray;

Now, the two objects are referring to the same array, because they're referring to the same 'location' in memory (you copied its reference). Indeed, if you modify an element of myArray in this way:

现在,这两个对象引用的是同一个数组,因为它们引用的是内存中相同的“位置”(您复制了它的引用)。事实上,如果你用这种方式修改myArray的一个元素:

myArray[0] = 123;

also the element of myStoredArrayRef will change:

myStoredArrayRef元素也会改变:

Console.WriteLine(myStoredArrayRef[0]); //it will print '123';

You're able also to store a reference to a List, because they're reference types (as rightly pointed out by @DavidHeffernan) like the arrays, with the same procedure.

您还可以存储对列表的引用,因为它们是引用类型(如@DavidHeffernan所正确指出的),类似于数组,使用相同的过程。