2

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.

Zshelley
  • 45
  • 6
  • do you mean `two-dimensional-array` or `hash-table` ? – zey Jan 15 '14 at 09:27
  • Do you mean you want two or more variables to point to the same array? – haughtonomous Jan 15 '14 at 09:27
  • I mean that I have an array somewhere and I need to be able to access it on the fly. Its outside the scope of where I need to access it, and I dont want to copy it. Im hoping that it is possible to just store a reference to that array so I can access it through that. – Zshelley Jan 15 '14 at 09:29
  • Not very OOP. I think you shouldn't think about the data itself but about the functionality that is built using that data and provide a class (also a static class) that provide the required functionality based on the data stored in that array – Steve Jan 15 '14 at 09:33
  • Well if you assign it to another variable (eg var tempArray=MyArray) and pass tempArry around as an argument, it will 'point' to the array. I believe array is a reference type so tempArry and MyArray are effectively pointers anyway. – haughtonomous Jan 15 '14 at 09:40

2 Answers2

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:

int[] b = a;

Now a and b refer to the same array.

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I dident think it worked like that. Had been assuming b=a would just make a copy of all the data in a into b. Will try now, thanks! – Zshelley Jan 15 '14 at 09:30
  • 1
    @Zshelley Arrays are reference types.. therefore the reference is copied.. not the data they reference. – Simon Whitehead Jan 15 '14 at 09:39
  • A copy is only made for a value type. Reference type variables (all objects) are pointers to objects on the heap - in C# they just don't look like pointers. In David's example, a and b are pointers. However, int a = 1; int b = a; ( int is a value type) defines two variables with separate copies of the value 1. You need to understand clearly the difference between value and reference types. – haughtonomous Jan 15 '14 at 09:43
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.

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.

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[0] = 123;

also the element of myStoredArrayRef will change:

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.

Community
  • 1
  • 1
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61