Created
March 31, 2021 13:58
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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