Based on your explanation you are using custom class as your key.
When using reference types as key you need to make sure that you use a reference to the same object that is being referenced by the dictionary key.
For example if you have this type
public class Skill
{
public string SkillName
{
get;
set;
}
}
Then you use it this way
Dictionary<Skill, object> dic = new Dictionary<Skill, object>();
for (int i = 0; i < 5; i++)
{
dic.Add(new Skill() { SkillName = i.ToString() }, new object());
}
Skill lookup = new Skill() { SkillName = "0"};
Console.WriteLine(dic.ContainsKey(lookup));
This will return false because lookup reference is pointing to an object which is different from the object that has been created in the loop, notice that the SkillName has the same value ("0" in this case).
If you change the code to the following
Dictionary<Skill, object> dic = new Dictionary<Skill, object>();
for (int i = 0; i < 5; i++)
{
dic.Add(new Skill() { SkillName = i.ToString() }, new object());
}
Skill lookup = dic.Keys.FirstOrDefault(sk => sk.SkillName == "0");
Console.WriteLine(dic.ContainsKey(lookup));
You will notice that the console line is printing true now. This is because the lookup reference is pointing to the same object that the dictionary has.
How does this related to your question? Its the serialization and de-serialization. If you create an object of type Skill like this
Skill skill1 = new Skill(){SkillName = "C#"};
Then serialize it using a serializer of your choice
Serializer.Serialize(skill1,streem);
Then you de-serialize it like this
Skill skill2 = Serializer.Deserialize(stream);
And print skill1 == skill2
you will find that it will return false, this is because what the serializer did when de-serializing the object is that it created new object and populated it with the same values that the first object had.
The rule is very simple reference equality is based on pointing to the same object, not on two different objects having the same property values.
Wish I have hit the spot given that you didn't provide enough information