Skip to content

Instantly share code, notes, and snippets.

@sasan-salem
Created March 31, 2021 13:58
Show Gist options
  • Save sasan-salem/f4f281e30342a336a87064c7991ca652 to your computer and use it in GitHub Desktop.
Save sasan-salem/f4f281e30342a336a87064c7991ca652 to your computer and use it in GitHub Desktop.
class Student
{
public string Name { get; set; }
public int Number { get; set; }
public int SerialNo { get; set; }
public int Grade { get; set; }
public static IEqualityComparer<Student> SerialNoComparer { get => new SerialNoEqualityComparer(); }
public static IEqualityComparer<Student> NumberComparer { get => new NumberEqualityComparer(); }
private sealed class SerialNoEqualityComparer : IEqualityComparer<Student>
{
public bool Equals(Student s1, Student s2)
{
if (s1 == null && s2 == null)
return true;
else if (s1 == null || s2 == null)
return false;
else if (s1.SerialNo == s2.SerialNo)
return true;
else
return false;
}
public int GetHashCode(Student student)
{
return student.SerialNo.GetHashCode();
}
}
private sealed class NumberEqualityComparer : IEqualityComparer<Student>
{
public bool Equals(Student s1, Student s2)
{
if (s1 == null && s2 == null)
return true;
else if (s1 == null || s2 == null)
return false;
else if (s1.Number == s2.Number)
return true;
else
return false;
}
public int GetHashCode(Student student)
{
return student.Number.GetHashCode();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment