3

Say I have a simple class like this:

public class ReferenceChanger<T> 
{
    public T SavedElement { get; private set; }

    public ReferenceChanger(T elementToSave)
    {
        SavedElement = elementToSave;
    }

    // This method would change the original variable (given as a constructor argument)
    public void SetNewReference(T newElement)
    {
        SavedElement = newElement;
    }
}

This class saves an element given to its constructor, whatever element is. However, the "SavedElement" (its backing field) is a reference to the object given at the time of instance creation.

Is there any way to save a reference to a variable (as with using ref keyword), so that if the original item passed to the constructor changes, the SavedElement would automatically reflect the change, almost as if the object was passed with the ref keyword? (Even if I use the ref keyword, I would not be able to save the reference that way.)

Updated to make intentions more clear:

public class ExampleClass 
{
    public List<int> Numbers { get; set; }
}

public static void Main()
{
    ExampleClass temp = new ExampleClass();
    temp.Numbers = new List<int>() { 1, 2, 3 }; 

    ReferenceChanger<List<int>> changer = new ReferenceChanger<List<int>>(temp.Numbers);
    // Here, a reference to the List<int> instance (containing 1,2,3) is stored in changer's SavedElement

    // Then I do this:
    changer.SetNewReference(new List<int>() { 5, 6, 7 });

    // Now, only changer's SavedElement was changed, but temp's property Numbers was not changed.
    // Is there a way to change the temp's property Numbers from the changer?
}
12
  • 1
    seeing that classes are automatically reference types, in change in the object passed to your ctor should be reflected in your SavedElement, as it's merely referencing that object, it's not being passed by value, but by reference. Sep 6, 2011 at 21:06
  • Do you mean to say that you want code that consumed the value of SavedElement before the call to SetNewReference to subsequently carry out operations on the new version of SavedElement?
    – Reddog
    Sep 6, 2011 at 21:12
  • If so, just pass the whole instance of ReferenceChanger to the operation and have it call SavedElement when it needs to. It'll always use the "latest" reference.
    – Reddog
    Sep 6, 2011 at 21:12
  • @Tony I think you have misunderstood my question. I will add some more code that will describe it. Changes in the referenced object will be visible, but not changes OF THE OBJECT in its variable. Sep 6, 2011 at 21:13
  • 1
    The commenters are missing the point of the question. 1) T is not constrained to a reference type. 2) He wants to capture a reference to the variable (he cannot do this, for the record), so that when the variable changes, he'll see it. @Kornelije, if you can confirm that you want to observe changes to the variable, and not just the variable's original object, that might help. Or maybe I'm the one missing the point, you can confirm that, too. Sep 6, 2011 at 21:18

3 Answers 3

4

Sounds like you're looking for TypedReference and the __makeref keyword.

Warning: they're poorly documented and not in the standardized part of C#.

There's a lot more information in this question.

0
0

All classes in C# are reference objects so what you have coded should update the value of SavedElement. However, if T is a primitive type (e.g., int, string, etc.), this would not work since these are set by value. You would need to put a constraint on T to make sure it's a class.

1
  • one slight correction - string is a reference type, it just happens to be an immutable reference type. You can have two string variables pointing to the exact same instance of a string. Sep 6, 2011 at 21:18
0

You cannot normally capture a reference to a variable and store it as a property. One hackish solution (not really suggesting it's a good idea, I'd explore other avenues first) is to capture it in a closure and pass the closure around. Closures capture variables, not values. As a result, changes to variables can be observed elsewhere. For example, given

class Foo
{
    public int Baz { get; set; }
}

class Bar
{
    private Func<Foo> fooGetter;

    public Bar(Func<Foo> fooGetter)
    {
        this.fooGetter = fooGetter;
    }

    public void Do()
    {
        Console.WriteLine(fooGetter().Baz);
    }
}

You can have

Foo foo = new Foo() { Baz = 1 };
Bar bar = new Bar(() => foo);
bar.Do();
foo = new Foo() { Baz = 2 };
bar.Do();

Changes to the variable foo are observed, since that is what the caller enclosed in the lambda. Had the caller simply said () => new Foo(), you would (of course) not observe any changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.