Gerard
Gerard

Reputation: 2904

Reference to a reference in C#

Supose I create a variable B that references another one, A, both of them being reference type variables. If I set B or A to null, the other one would still be pointing to the object instance, that would remain untouched.

SomeClass A = new SomeClass();
SomeClass B = A;
B = null;                       //A still has a valid reference

This is also true:

SomeClass A = new SomeClass();
SomeClass B = A;
A = null;                       //B still has a valid reference

But I don´t want B to reference the instance referenced by A, I want B to reference A itself. That way, if B was set to null, A would be set to null as well. Is there any elegant, safe(no pointers) way of doing this? or am I trying to do something that is against C# principles?

Thanks.

Upvotes: 2

Views: 139

Answers (3)

Servy
Servy

Reputation: 203850

The solution here is for neither of those variables to directly refer to the object, but to instead refer to an object instance that has a field pointing to an actual SomeClass instance:

public class Pointer<T>
{
    public T Value {get;set;}
}

Pointer<SomeClass> A = new Pointer<SomeClass>(){ Value = new SomeClass()};
Pointer<SomeClass> B = A;
B.Value = null;    
//A.Value is null

Upvotes: 1

Anil Mathew
Anil Mathew

Reputation: 2696

MSDN has the following definition for a reference type: "Variables of reference types store references to the actual data" (http://msdn.microsoft.com/en-us/library/490f96s2%28v=vs.110%29.aspx). In your case, setting the second variable to null is only causing the reference of the second variable to be broken without having any effect on the actual data. This is clearly shown in the post by Olivier available at Setting a type reference type to null doesn't affect copied type?.

A possible solution to your problem is to make use of a WeakReference.

According to MSDN: "A weak reference allows the garbage collector to collect an object while still allowing an application to access the object. If you need the object, you can still obtain a strong reference to it and prevent it from being collected" (http://msdn.microsoft.com/en-us/library/system.weakreference%28v=vs.110%29.aspx).

So as long as the second (local) reference is accessing your weak reference, the object won't be garbage collected. Once you break the local reference by setting that to null, the GC would clear the weakly referenced object. More information on WeakReference is available at: http://msdn.microsoft.com/en-us/library/ms404247.aspx

Upvotes: 0

John K&#228;ll&#233;n
John K&#228;ll&#233;n

Reputation: 7993

You can't do this the way you would do it in C++ or C. The only time you can have a reference to an object handle is when you call a method with a ref parameter: viz:

void main_method()
{
    SomeClass A = new SomeClass();
    secondary_method(ref A);
}

void secondary_method(ref SomeClass B)
{
    B = null;   // this has the side effect of clearing the A of main_method
}

Upvotes: 9

Related Questions