-3

Why is the object retrieved from the Dictionary by key not a reference?

My test code:

class Program
{
    static Dictionary<int, Salad> test = new Dictionary<int, Salad>();
    static void Main(string[] args)
    {
        test.Add(1, new Salad() { Vegetables = Vegetables.Tomato });
        test.Add(2, new Salad() { Vegetables = Vegetables.Tomato });
        var newSalad = new Salad() { Vegetables = Vegetables.Carrot };
        test[1] = newSalad;
        var salad2 = test[2];
        salad2 = newSalad;
        Console.WriteLine(test[1].Vegetables);
        //Write: Carrot
        Console.WriteLine(test[2].Vegetables);
        //Write: Tomato
        Console.ReadLine();
    }
    
}
public class Salad
{
    public Vegetables Vegetables { get; set; }
}
public enum Vegetables
{
    Tomato,
    Potato,
    Carrot
}
Silny ToJa
  • 1,815
  • 1
  • 6
  • 20
  • It's not clear to me what you're asking. What different output were you expecting and why? The objects you're reading from the `Dictionary` are reference types (`Salad` instances). But it's not clear why that's an issue here. – David Aug 03 '21 at 11:33
  • @David It think they expect assigning a new reference to `salad2` should also assign it to `test[2]`. – juharr Aug 03 '21 at 11:35
  • "Why is the object retrieved from the Dictionary by key not a reference?" - It is a reference. `Salad` is a class so it is a reference type. – Enigmativity Aug 03 '21 at 11:36
  • You should read [ask]. – Enigmativity Aug 03 '21 at 11:37
  • @David I mean what wrote juharr – Silny ToJa Aug 03 '21 at 11:37
  • Assigning a reference to a variable and then assigning it again does not change the first source object... you only point toward another object and lost the first connection. Thus it does not change the sotred item (reference) in the collection. –  Aug 03 '21 at 11:38
  • @SilnyToJa That's like expecting `int a = 1; int b= a; b=3;` resulting in `a` being 3. – juharr Aug 03 '21 at 11:39
  • Does this answer your question? [What exactly is a reference in C#](https://stackoverflow.com/questions/40686776/what-exactly-is-a-reference-in-c-sharp) and [What is the difference between a C# Reference and a Pointer?](https://stackoverflow.com/questions/430112/what-is-the-difference-between-a-c-sharp-reference-and-a-pointer) –  Aug 03 '21 at 11:39

1 Answers1

1

You seem to be slightly confused as to how references in c# actually work.
When assigning a new value to a reference, you change the instance that reference points to, but any other references to that instance still points to the same instance.
salad2 and test[2] both starts as references to the same instance, but then you have this row: salad2 = newSalad;.

This row doesn't change the reference stored in the dictionary, it changes the salad2 reference and have it point to the same Salad instance that newSalad points to.

The test[2] reference is not changed.

If your code would have salad2.Vegetables = newSalad.Vegetables you would get Tomato in both Console.WriteLine() calls.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121