"The problem with dictionary is that if i change the order of the instances, then even the ID changes."
That statement does not make sense. While it is correct that you should
never rely on the KeyValuePairs in a Dictionary being in the same "order" you put them in (or, any order), using a Dictionary guarantees ... by virtue of the fact it does not allow duplicate Keys ... that each key is unique.
I think (guess) what you want is a unique id (int) for each instance of the Class; one way you can do that is:
using System.Collections.Generic;
namespace YourNameSpace
{
public class AB
{
private static int AB_ID = 0;
public static Dictionary<AB, int> ABInstanceToID = new Dictionary<AB, int>();
public int ID { set; get; }
public AB()
{
this.ID = AB_ID;
ABInstanceToID.Add(this, this.ID);
AB_ID++;
}
}
}
So, given a run-time instance of AB, you can read its ID by doing a lookup in the static Dictionary:
if(AB.ABInstanceToID.ContainsKey(someAB))
{
int index = AB.ABInstanceToID[someAB];
switch(index)
{
case 0:
break;
default:
break;
}
}
else
{
?>
}
Note:
1.in "theory" the 'ContainsKey test should not be necessary, but I think it's always a good idea to check and re-check, and plan for errors.
2. while some might favor using Dictionary.TryGetValue for the check, in this case, where we can assume frequent attempts at look-up of missing Keys is very improbable, I see no reason to use that, here.