Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class Comparable<T> : IComparable, IComparable<T>, IEquatable<T> where T: Comparable<T> {
- public abstract override int GetHashCode();
- public abstract int CompareTo(T other);
- public int CompareTo(object obj) {
- T other = obj as T;
- if (other == null && obj != null) {
- throw new ArgumentException($"Objects of type {typeof(T).Name} can only be compared to objects of the same type", nameof(obj));
- }
- return CompareTo(other);
- }
- public override bool Equals(object obj) {
- return CompareTo(obj) == 0;
- }
- new public bool Equals(T other) {
- return CompareTo(other) == 0;
- }
- private static int Compare(Comparable<T> comp1, Comparable<T> comp2) {
- if (comp1 == null) {
- return ((comp2 == null) ? 0 : -1);
- }
- return comp1.CompareTo(comp2);
- }
- public static bool operator == (Comparable<T> comp1, Comparable<T> comp2) {
- return Compare(comp1, comp2) == 0;
- }
- public static bool operator != (Comparable<T> comp1, Comparable<T> comp2) {
- return Compare(comp1, comp2) != 0;
- }
- public static bool operator > (Comparable<T> comp1, Comparable<T> comp2) {
- return Compare(comp1, comp2) > 0;
- }
- public static bool operator < (Comparable<T> comp1, Comparable<T> comp2) {
- return Compare(comp1, comp2) < 0;
- }
- public static bool operator >= (Comparable<T> comp1, Comparable<T> comp2) {
- return Compare(comp1, comp2) >= 0;
- }
- public static bool operator <= (Comparable<T> comp1, Comparable<T> comp2) {
- return Compare(comp1, comp2) <= 0;
- }
- }
- public class SeasonCompare : Comparable<SeasonCompare> {
- public int Number {get; set;}
- public override int GetHashCode() {
- return Number;
- }
- public override int CompareTo(SeasonCompare other) {
- if (other == null) {
- return 1;
- }
- return Number.CompareTo(other.Number);
- }
- }
Add Comment
Please, Sign In to add comment