Below is a sample of my dictionary definition. When I define DicKeys
as a class
, ContainsKey()
doesn't work. If I change DicKeys
to a struct
, it works properly.
Why does ContainsKey()
work differently for a class
than it does for a struct
?
Dictionary<DicKeys, DicVals> aDic = new Dictionary<DicKeys, DicVals>();
// original version as below
public Class DicKeys
{
public EnKey1 DicKeyItem1,
public EnKey2 DicKeyItem2,
}
// revised version as below
public struct DicKeys
{
public EnKey1 DicKeyItem1,
public EnKey2 DicKeyItem2,
}
// common parts of code below
public enum EnKey1
{
A1,
A2,
}
public enum EnKey2
{
B11,
B12,
}
DicKeys aDicKey = new DicKeys();
// assigned value to aDicKey obj here;
if (aDic.ContainsKey(aDicKey ) == true)
{
// do some thing here
// If I defined as 'class', it doesn't hit here.
// Updated to 'struct', it hit here.
}