Background
I understand a C# dictionary is a data structure that represents a collection of key|value pairs.
The below code (LINQPad C# Program) and subsequent output screenshot demonstrates how I am attempting to use a dictionary to (1) retain the most recent two instances of a class (RefInteger) in order of creation, and (2) allow for the "deletion" of the most recent RefInteger instance. Where :
refDict[A]returns the most recent created instance ofRefInteger,refDict[B]returns the second most recent created instance, andrefDict[C]is only required to "resupply"refDict[B]in the event of the most recentRefIntegerinstance is "deleted" (via theRegressEntriesmethod).
Code:
public enum Pos { A, B, C }
public Dictionary<Pos, RefInteger> refDict = new Dictionary<Pos, RefInteger>();
void Main()
{
Create(1);
refDict.Dump("Create 1");
Create(2);
refDict.Dump("Create 2");
Create(3);
refDict.Dump("Create 3");
RegressEntries();
refDict.Dump("Regress");
Create(4);
refDict.Dump("Create 4");
Create(5);
refDict.Dump("Create 5");
}
private void Create(int value)
{
ProgressEntries();
refDict[Pos.A] = new RefInteger(value);
}
private void ProgressEntries()
{
if (refDict.ContainsKey(Pos.B))
refDict[Pos.C] = refDict[Pos.B];
if (refDict.ContainsKey(Pos.A))
refDict[Pos.B] = refDict[Pos.A];
}
private void RegressEntries()
{
if (refDict.ContainsKey(Pos.B))
refDict[Pos.A] = refDict[Pos.B];
if (refDict.ContainsKey(Pos.C))
refDict[Pos.B] = refDict[Pos.C];
}
public class RefInteger
{
public int Value;
public RefInteger(int value)
{
Value = value;
}
}
Screenshot

Question
While the above simplified example behaves as I expect a dictionary to behave, I have dictionary where the ProgressEntries method appears to result in each subsequent refDictkey pointing to the most recent value. It would be the equivalent of receiving the below output from the simplified example:

Please suggest how this is possible.
Noting, I have confirmed the order of "progression" within the ProgressEntries method is correct (i.e. move refDict[B] to refDict[C] THEN move refDict[A] to refDict[B]).
The problem dictionary while small, uses a composite key to return values. Moving to another structure is not really an option.
The example code uses a simple class (refInteger) as opposed to a primitive type to ensure my understanding of dictionary behavior is correct.
Thanks
Shannon
EDIT: Please find below the requested example console app code. The example behaves as expected. I will keep digging into the issue with the actual code.
using System;
using System.Collections.Generic;
namespace DictionaryProgress
{
class Program
{
public enum Pos { A, B, C }
static void Main()
{
var refDictionary = new RefDictionary();
refDictionary.Update();
Console.ReadKey();
}
public class RefDictionary
{
public Dictionary<Pos, RefInteger> RefDict = new Dictionary<Pos, RefInteger>();
public void Update()
{
Create(1);
Console.WriteLine("Create 1 : {0}", ToString());
Create(2);
Console.WriteLine("Create 2 : {0}", ToString());
Create(3);
Console.WriteLine("Create 3 : {0}", ToString());
RegressEntries();
Console.WriteLine("Regress : {0}", ToString());
Create(4);
Console.WriteLine("Create 4 : {0}", ToString());
Create(5);
Console.WriteLine("Create 5 : {0}", ToString());
}
private void Create(int value)
{
ProgressEntries();
RefDict[Pos.A] = new RefInteger(value);
}
private void ProgressEntries()
{
if (RefDict.ContainsKey(Pos.B))
RefDict[Pos.C] = RefDict[Pos.B];
if (RefDict.ContainsKey(Pos.A))
RefDict[Pos.B] = RefDict[Pos.A];
}
private void RegressEntries()
{
if (RefDict.ContainsKey(Pos.B))
RefDict[Pos.A] = RefDict[Pos.B];
if (RefDict.ContainsKey(Pos.C))
RefDict[Pos.B] = RefDict[Pos.C];
}
public override string ToString()
{
var PosA = RefDict.ContainsKey(Pos.A) ?
RefDict[Pos.A].Value : 0;
var PosB = RefDict.ContainsKey(Pos.B) ?
RefDict[Pos.B].Value : 0;
var PosC = RefDict.ContainsKey(Pos.C) ?
RefDict[Pos.C].Value : 0;
return string.Format("{0}, {1}, {2}", PosA, PosB, PosC);
}
}
public class RefInteger
{
public int Value;
public RefInteger(int value)
{
Value = value;
}
}
}
}