This file contains hidden or 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 : IComparable<Student> | |
| { | |
| public string Name { get; set; } | |
| public int Number { get; set; } | |
| public int SerialNo { get; set; } | |
| public int Grade { get; set; } | |
| public int CompareTo(Student other) | |
| { | |
| if (other.SerialNo < SerialNo) |
This file contains hidden or 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 SerialNoComparer : IComparer<Student> | |
| { | |
| public int Compare(Student s1, Student s2) | |
| { | |
| return s1.SerialNo.CompareTo(s2.SerialNo); | |
| } | |
| } |
This file contains hidden or 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(); } |
This file contains hidden or 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
| List<Student> students = new List<Student>(); | |
| // Add some students to this List | |
| var specificStudent = new Student() { SerialNo = 12, Number = 34, Grade = 3, Name = "Joe" }; | |
| bool exist1 = students.Contains(specificStudent, new NumberEqualityComparer()); | |
| bool exist2 = students.Contains(specificStudent, new SerialNoEqualityComparer()); |
This file contains hidden or 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 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; |
This file contains hidden or 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 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; |
This file contains hidden or 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 : IEquatable<Student> | |
| { | |
| public string Name { get; set; } | |
| public int Number { get; set; } | |
| public int SerialNo { get; set; } | |
| public int Grade { get; set; } | |
| public bool Equals(Student other) | |
| { | |
| if (other.SerialNo == SerialNo) |
This file contains hidden or 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 : IEquatable<Student> | |
| { | |
| public string Name { get; set; } | |
| public int Number { get; set; } | |
| public int SerialNo { get; set; } | |
| public int Grade { get; set; } | |
| public bool Equals(Student other) | |
| { | |
| if (other.SerialNo == SerialNo) |
This file contains hidden or 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
| bool result = false; | |
| if (student1.GetHashCode() == student2.GetHashCode()) | |
| result = student1.Equals(student2); |
This file contains hidden or 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
| Student student1 = new Student() | |
| { | |
| Name = "Joe", | |
| Grade = 3, | |
| Number = 10, | |
| SerialNo = 3213214 | |
| }; | |
| Student student2 = new Student() | |
| { |
NewerOlder