Storing a reference to an object in C#

Posted by Jack on Stack Overflow See other posts from Stack Overflow or by Jack
Published on 2010-05-03T17:58:07Z Indexed on 2010/05/03 18:08 UTC
Read the original article Hit count: 278

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET