Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

References to variables in C#?

Tags:

c#

In C++, I can do something like this:

int i[10] = new int[10];
int *p = &i[5];

Then, I can always know that p points to the 5th element of int array i, regardless of i's contents.

Is there any way to do something similar in C#?

I realize this is likely one of the ways in which C# "protects" us from ourselves, so i'm not looking for an exact equivelent, but rather a similar concept... that is, being able to refer to the contents of some other variable, rather than the instance of the variable itself.

Here's my use case i'm thinking of. I have an array of strings. I would like to have another array of references to those array elements. Something like this (obviously not valid code):

string[] s = new string[] { "one", "two", "three", "four", "five", "six" };
stringref[] sr = new stringref[] { &s[0], &s[1], &s[2], &s[3], &s[4], &s[5] };

Console.WriteLine(sr[1]); // == "two"
s[1] = "two point zero";
Console.WriteLine(sr[1]); // == "two point zero"

Certainly, ref paramets do this, and out parameters allow you to write to a specific variable. But what about non-parameters? Can you store a ref? Can you keep an array of refs or a dictionary?

It seems like if the ability to do it with parameters is present, there should be a way to do it without them.

like image 943
Erik Funkenbusch Avatar asked Sep 14 '09 07:09

Erik Funkenbusch


People also ask

What are reference variables?

A reference variable is a variable that points to an object of a given class, letting you access the value of an object. An object is a compound data structure that holds values that you can manipulate. A reference variable does not store its own values.

Can you pass variables by reference in C?

Pass by Reference A reference parameter "refers" to the original data in the calling function. Thus any changes made to the parameter are ALSO MADE TO THE ORIGINAL variable. Arrays are always pass by reference in C.

How do you assign a reference to a variable?

To assign reference to a variable, use the ref keyword. A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters. Declare the reference parameters using the ref keyword.

How do references work in C?

The main use of references is acting as function formal parameters to support pass-by-reference. In an reference variable is passed into a function, the function works on the original copy (instead of a clone copy in pass-by-value). Changes inside the function are reflected outside the function.


1 Answers

No. Putting unsafe code aside, which does allow holding pointers to memory locations, there's no way to store a reference to a variable in C#.

ref and out arguments provide the only means to take a reference but you can't save them anywhere.

You can workaround this limitation by wrapping fields in a class and using its reference instead. This is what the compiler does to capture variables in closures:

For instance, when you write:

int integer = 0;
Action<int> method = i => Console.WriteLine(i + integer);
integer = 42;
method(100); // prints 142, not 100

In the second line, the compiler will have to take out the anonymous method and store it as a separate method in the class. Obviously, that method won't have access to integer variable. It somehow needs to pass a "reference" to integer variable to that anonymous method. Since it's not possible, it'll generate a class with a field to hold an integer and uses an instance of that class to store the variable. Basically, the local variable is promoted to a field in a class and is stored in the heap.

like image 117
mmx Avatar answered Sep 22 '22 13:09

mmx