79745277

Date: 2025-08-25 01:49:00
Score: 0.5
Natty:
Report link

When you do dict[1] = new Value(...) you’re replacing the object, so any variable that referenced the old object won’t see the update.

If you want both to always stay in sync even when you replace the value, you can wrap the object in a reference container:

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

class Value
{
    public int Hello { get; set; }
    public string Bye { get; set; }
}

class Program
{
    static void Main()
    {
        var dict = new Dictionary<int, Ref<Value>>();

        var v = new Ref<Value> { Value = new Value { Hello = 1, Bye = "QWERTY" } };
        dict.Add(1, v);

        Ref<Value> c = dict[1];
        Console.WriteLine(c.Value.Bye); // QWERTY

        // Replace the inner value, reference stays the same
        dict[1].Value = new Value { Hello = 2, Bye = "ASDF" };
        Console.WriteLine(c.Value.Bye); // ASDF
    }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When you
  • Low reputation (1):
Posted by: Joker