Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a reference in c#

I'm trying to design a class which can update a reference to an object (outside the class) on destruction.

So essentially you create an instance of this object and pass it a reference type (in whichever manner, constructor etc.) and then on destruction of the object the original reference has changed to a reference created by the object.

If I pass a reference by reference (say in construction) I can't figure a way to store this reference (as a reference) for the destructor to update it? For example (pseudo):

class Updater
{
    object privateReference;
    public Updater(ref object externalReference)
    {
        privateReference = externalReference; //is privateReference now a new reference to the original object?
    }

    ~Updater()
    {
        privateReference = new object(); //therefore this isn't 'repointing' the externalReference
    }
}

The key here is I'm not trying to mutate the original 'external' object from this class I'm trying to 'repoint' it, or initialise it if you will.

like image 624
Adam Naylor Avatar asked Dec 08 '09 11:12

Adam Naylor


3 Answers

You can't do this, basically. ref is only applicable within the method itself, effectively.

It's not at all clear what you want to use this for - could you give us more information so we can suggest alternative designs? Anything which relies on a finalizer is troubling to start with, to be honest...

like image 116
Jon Skeet Avatar answered Nov 18 '22 18:11

Jon Skeet


This won't work because the ref aspect of the constructor parameter only applies inside the constructor.

What I would do is to give the constructor a delegate that can be used to update the thing in question. Also, you should use IDisposable for this (in conjunction with a using block) rather than a finalizer; finalizers shouldn't touch managed objects (they're designed to free unmanaged objects).

class Updater : IDisposable
{
    Action<object> setter;

    public Updater(Action<object> setter)
    {
        this.setter = setter;
    }

    public Dispose()
    {
        setter(new object());
    }
}
like image 32
Tim Robinson Avatar answered Nov 18 '22 20:11

Tim Robinson


Be aware that using a Finalizer like this isn't the same as a destructor in C++.

When the last reference dies (perhaps it it goes out of scope) the object goes onto the Finalizer queue. The GC determines when to call the Finalizer and it could be a long time after the last reference dies.

like image 2
Matt Breckon Avatar answered Nov 18 '22 18:11

Matt Breckon