Guest User

Untitled

a guest
Dec 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. public abstract class Comparable<T> : IComparable, IComparable<T>, IEquatable<T> where T: Comparable<T> {
  2. public abstract override int GetHashCode();
  3.  
  4. public abstract int CompareTo(T other);
  5.  
  6. public int CompareTo(object obj) {
  7. T other = obj as T;
  8. if (other == null && obj != null) {
  9. throw new ArgumentException($"Objects of type {typeof(T).Name} can only be compared to objects of the same type", nameof(obj));
  10. }
  11. return CompareTo(other);
  12. }
  13.  
  14. public override bool Equals(object obj) {
  15. return CompareTo(obj) == 0;
  16. }
  17.  
  18. new public bool Equals(T other) {
  19. return CompareTo(other) == 0;
  20. }
  21.  
  22. private static int Compare(Comparable<T> comp1, Comparable<T> comp2) {
  23. if (comp1 == null) {
  24. return ((comp2 == null) ? 0 : -1);
  25. }
  26. return comp1.CompareTo(comp2);
  27. }
  28.  
  29. public static bool operator == (Comparable<T> comp1, Comparable<T> comp2) {
  30. return Compare(comp1, comp2) == 0;
  31. }
  32.  
  33. public static bool operator != (Comparable<T> comp1, Comparable<T> comp2) {
  34. return Compare(comp1, comp2) != 0;
  35. }
  36.  
  37. public static bool operator > (Comparable<T> comp1, Comparable<T> comp2) {
  38. return Compare(comp1, comp2) > 0;
  39. }
  40.  
  41. public static bool operator < (Comparable<T> comp1, Comparable<T> comp2) {
  42. return Compare(comp1, comp2) < 0;
  43. }
  44.  
  45. public static bool operator >= (Comparable<T> comp1, Comparable<T> comp2) {
  46. return Compare(comp1, comp2) >= 0;
  47. }
  48.  
  49. public static bool operator <= (Comparable<T> comp1, Comparable<T> comp2) {
  50. return Compare(comp1, comp2) <= 0;
  51. }
  52. }
  53.  
  54. public class SeasonCompare : Comparable<SeasonCompare> {
  55. public int Number {get; set;}
  56.  
  57. public override int GetHashCode() {
  58. return Number;
  59. }
  60.  
  61. public override int CompareTo(SeasonCompare other) {
  62. if (other == null) {
  63. return 1;
  64. }
  65. return Number.CompareTo(other.Number);
  66. }
  67. }
Add Comment
Please, Sign In to add comment