Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a reference to an object in C#

Tags:

c#

.net

I was wondering how one could store a reference to an object in .net.

That is, I would like something like the following code (note, of course, that the following code may be way off from how to actually do it):

class Test
{
    private /*reference to*/ Object a;
    public Test(ref int a)
    {
        this.a = a;
        this.a = ((int)this.a) + 1;
    }
    public Object getA() { return this.a; }
}
/*
 * ...
 */
static void Main(string[] args)
{
    int a;
    a=3;
    Test t = new Test(ref a);
    Console.WriteLine(a);
    Console.WriteLine(t.getA());
    Console.ReadKey();
}

To produce the following output:

4
4

Ideally, I would like to do this without writing a wrapper class around the integer.

In other words, I think I want pointers in .Net.

like image 746
Jack Edmonds Avatar asked May 03 '10 17:05

Jack Edmonds


People also ask

Where is object reference stored C#?

In C#, classes and interfaces are reference types. Variables of reference types store references to their data (objects) in memory, and they do not contain the data itself. An object of type Object , string , or dynamic is also a reference type. // In this code, sc and sc2 refer to the same object.

How do I save a reference in C++?

There are two legal ways to store the variable by reference: // o1 is const const Object& o1 = Object(); // o2 is mutable Object&& o2 = Object(); There is a special rule in c++ that keeps the Object alive for as long as the reference is in scope.

What is reference to object?

A reference is an address that indicates where an object's variables and methods are stored. You aren't actually using objects when you assign an object to a variable or pass an object to a method as an argument. You aren't even using copies of the objects. Instead, you're using references to those objects.

What is reference object example?

A variable whose type is a class contains a reference to an object of the class (i.e., the address of the memory location where the object is allocated). Example: String s; s = "xxx"; The first statement declares a variable s of type String.


2 Answers

You cannot store references to variables in .NET, period. You can store references to objects, but not references to variables.

The reason is that if you were allowed to store references to arbitrary variables then you could store references to local variables. If you can store references to local variables then the runtime cannot use the optimization of storing local variables on the short-lived memory pool, aka, the stack.

Now, even if you could do that, the operation you are describing is not typesafe for a different reason. You have a (very badly named) field variable "a" of type "reference to object variable" and a (very badly and confusingly named) local variable "a" of type "reference to int variable". Even if you could store a reference to a variable it doesn't make any sense to store a reference to an int variable in something of type "reference to object variable" because those two types are logically not compatible. The operations you can perform on them are different; a reference to an object variable can have a string written into it; a reference to an int variable cannot.

Perhaps I am misunderstanding but wouldn't a variable such as the integer above be boxed into an object which could then be stored as a reference?

You are confusing references to objects with references to variables. It is confusing that we use the same terminology for what is really two different things.

Yes, boxing turns a value type, like int, into a reference type, like object. That has ABSOLUTELY NOTHING WHATSOEVER to do with references to variables.

When you make a ref to a variable you are making an alias for that variable. When you say

void M(ref int y) { y = 123; }
...
int x = 0;
M(ref x);

you are saying "x and y are two different names for the same variable".

Now, if what you want to do is represent the notion of "I have captured a variable and I want to be able to read and write it" then use delegates:

class Ref<T>
{
    private Func<T> getter;
    private Action<T> setter;
    public Ref(Func<T> getter, Action<T> setter)
    {
        this.getter = getter;
        this.setter = setter;
    }
    public T Value
    {
        get { return getter(); }
        set { setter(value); }
    }
}
...
int abc = 123;
var refabc = new Ref<int>(()=>abc, x=>{abc=x;});
... now you can pass around refabc, store it in a field, and so on
refabc.Value = 456;
Console.WriteLine(abc); // 456
Console.WriteLine(refabc.Value); // 456

Make sense?

like image 182
Eric Lippert Avatar answered Oct 03 '22 14:10

Eric Lippert


C# has no concept of a reference variable akin to C++'s int& a. There are workarounds. One is to use closures:

class Test
{
    private Func<int> get_a;
    private Action<int> set_a;
    public Test(Func<int> get_a, Action<int> set_a)
    {
        this.get_a = get_a;
        this.set_a = set_a;
        this.set_a(this.get_a() + 1);
    }
    public Object getA() { return this.get_a(); }
}
/*
 * ...
 */
static void Main(string[] args)
{
    int a;
    a=3;
    Test t = new Test(() => a, n => { a = n; });
    Console.WriteLine(a);
    Console.WriteLine(t.getA());
    Console.ReadKey();
}

I'm not in front of VS, so please excuse any embarrassing faux pas.

like image 38
Marcelo Cantos Avatar answered Oct 03 '22 14:10

Marcelo Cantos