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
}