1

I found similar topics, but I could not solve the problem I has reading them.

I want to do in C# the same thing like in C is array of pointers, but I do not want to use in C# pointers because it requires to use "unsafe".

How to build an array of " ref to int" that when I change any element of that array then I will also change that what it refs to at the same time the variable that it points to ( Like in C - array of pointers).

Best regards, Chris

Krzysztof Bieda
  • 187
  • 1
  • 2
  • 10
  • You might have to build your own class to handle this. But, why isnt a normal array valid for you? – D. Ben Knoble Oct 07 '15 at 15:09
  • 2
    You can't, basically. What would you expect to happen if the array was populated with a ref to a local variable and that local variable was then popped off the stack? You could create your own "wrapper" class, probably generic, and maintain a `Wrapper` reference in the calling code and a `Wrapper[]` in the manipulating code... – Jon Skeet Oct 07 '15 at 15:13
  • You can use out parameter which is basically work work like as reference . – Pankaj Gupta Oct 07 '15 at 15:19

2 Answers2

3

If performance is not a concern, than one common workaround is to use lambda to capture the access to value type variable:

Sample:

class GetSetPair<T> 
{
    public Func<T> Get {get;set;}
    public Action<T> Set {get;set;}
}


var referencesToInt = new List<GetSetPair<int>>();

int value = 42;
referencesToInt.Add(new GetSetPair<int>{Get=()=>value, Set = v => value = v});
referencesToInt[0].Set(33);
Console.WriteLine(value); // 33
value = 22;
Console.WriteLine(referencesToInt[0].Get()); //22
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

Arrays are reference types, and therefore you can pass around references to that array. For example, given the following program:

public class Program
{
    public static Random rnd = new Random();
    public static int[] array2;

    public static void Main()
    {
        int[] array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        PrintArray(array);
        array[4] = rnd.Next();
        PrintArray(array);
        ModArray(array, 2);
        PrintArray(array);

        array2 = array;  //This makes array2 reference array1

        ModArray(array2, 8);  //Operate on the array2 reference
        PrintArray(array);    //Changes are reflected in array
        PrintArray(array2);   //And in array2

        Console.ReadKey(true);
    }

    public static void PrintArray(int[] array)
    {
        foreach (var e in array)
            Console.Write(e + ", ");
        Console.WriteLine();
    }

    public static void ModArray(int[] array, int i)
    {
        array[i] = rnd.Next();
    }
}

Will give the following output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, <- Original
1, 2, 3, 4, 744477516, 6, 7, 8, 9, 10, <- Modified in Main
1, 2, 102109069, 4, 744477516, 6, 7, 8, 9, 10, <- Modified in ModArray
1, 2, 102109069, 4, 744477516, 6, 7, 8, 1776657318, 10, <- array, after modifying array2
1, 2, 102109069, 4, 744477516, 6, 7, 8, 1776657318, 10, <- array2

So in a sense, you can pass around a reference to the array, modify that reference, and have it reflected in all of its copies.

The caveat here is that anybody who "owns" a reference to the original array cannot reassign that array (or resize it). It can modify the elements of the array, but it can't make the original array point to a new instance (which is what happens when it is resized). Also, as Jon Skeet mentioned in his comment, the danger is that if you were doing this in a different method other than Main, if the array goes out of scope, what would happen to array2?

See Also:

Is int[] a reference type or a value type?

Are arrays or lists passed by default by reference in c#?

Community
  • 1
  • 1
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37