I'm a total noob with c#, and cannot figure out why the same method works in different ways. I'm making a simple spreadsheet application, and am using a Dictionary of cells, where the key is a string name and the value is a Cell object:
public struct Cell
{
private string Name { get; }
public Object Content { get; set; }
public Cell(string n, Object o)
{
Name = n;
Content = o;
}
}
Now, I'll need to be able to easily add/change the contents of the cell, so I've been doing this:
Dictionary<string, Cell> cells = new Dictionary<string, Cell>();
// Assign new cell to 5.0 & print
cells.Add("a1", new Cell("a1", 5.0));
Console.WriteLine(cells["a1"].Content); // Writes 5
// Assign cell to new content & print
cells.TryGetValue("a1", out Cell value);
value.Content = 10.0;
Console.WriteLine(cells["a1"].Content); // Writes 5
Console.ReadKey();
Of course, the dictionary creates the new cell just fine, but when I use TryGetValue, the new content for the cell doesn't make it to the actual object I'm trying to get. I was expecting the second print to be 10. In debug, it seems like it instantiates a new Cell instead of getting the reference of the cell at hand.
I've used a Dictionary before, and I've used TryGetValue to change properties of the existing object. So here's two questions: What am I doing wrong in this case, and what factors determine if the method returns a reference or not?