I need to know the ways to compare many objects using hashcode. So here is one of the class.
public class Test: IEquatable<Test>
{
public Test()
{
}
public string ID { get; set; }
public string Name{ get; set; }
public static int SafeGetHashCode<T>(this T value) where T : class
{
return value == null ? 0 : value.GetHashCode();
}
public override int GetHashCode()
{
int hash = 19;
hash = hash * 31 + ID.SafeGetHashCode();
hash = hash * 31 + Name.SafeGetHashCode();
return hash;
}
public override bool isSame(object obj)
{
// compare objects here
}
}
But there are 2 errors. Both are same.
'string' does not contain a definition for 'SafeGetHashCode' and no extension method 'SafeGetHashCode' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
And for isSame(), I do not know how to write the code. the concept is compare all the objects and if there are 2 same ID & Name, group them together.
Item A = new Item();
Item B = new Item();
Item c = new Item();
A.ID = "Exam1";
A.Name = "Apple";
B.ID = "Exam1";
B.Name = "Orange";
C.ID = "Exam1";
C.Name = "Apple";
So Item A and C will group together.