Stu*_*ens 6 .net c# dictionary
我上了课 Column
public class Column
{
public int Id { get; private set; }
public string Name { get; private set; }
public EPersonalCharacteristicType Type { get; private set; }
public Column MainColumn { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
而且我在另一个班级中有以下字段
Dictionary<Column, string> dictionary
Run Code Online (Sandbox Code Playgroud)
在执行过程中,我得到了一个奇怪的行为dictionary.当我输入
dictionary.ContainsKey(dictionary.Keys.ToList()[1])
Run Code Online (Sandbox Code Playgroud)
我有false.
地球上怎么样?
UPDATED
我的Column类有GetHashCode或Equals Functions.以下是它们的实现:
public bool Equals(Column other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.Id == Id && Equals(other.Name, Name) && Equals(other.Type, Type) && Equals(other.MainColumn, MainColumn) && Equals(other.childColumns, childColumns);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Column)) return false;
return Equals((Column) obj);
}
public override int GetHashCode()
{
unchecked
{
int result = Id;
result = (result*397) ^ (Name != null ? Name.GetHashCode() : 0);
result = (result*397) ^ Type.GetHashCode();
result = (result*397) ^ (MainColumn != null ? MainColumn.GetHashCode() : 0);
result = (result*397) ^ (childColumns != null ? childColumns.GetHashCode() : 0);
return result;
}
}
public static bool operator ==(Column left, Column right)
{
return Equals(left, right);
}
public static bool operator !=(Column left, Column right)
{
return !Equals(left, right);
}
Run Code Online (Sandbox Code Playgroud)