elena1234

CompareTo

Feb 25th, 2021 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. using System;
  2. namespace GenericCountMethodString
  3. {
  4.     class Box<T> : IComparable<Box<T>> where T : IComparable<T>
  5.     {
  6.         public Box(T data)
  7.         {
  8.             this.Data = data;
  9.         }
  10.  
  11.         public T Data { get; set; }
  12.  
  13.         public int CompareTo(Box<T> otherBox)
  14.         {
  15.             if (this.Data.CompareTo(otherBox.Data) > 0)
  16.             {
  17.                 return 1;
  18.             }
  19.  
  20.             else if (this.Data.CompareTo(otherBox.Data) < 0)
  21.             {
  22.                 return -1;
  23.             }
  24.  
  25.             else // they are equal
  26.             {
  27.                 return 0;
  28.             }
  29.         }
  30.  
  31.         public override string ToString()
  32.         {
  33.             return $"{Data.GetType().FullName}: {this.Data}";
  34.         }
  35.     }
  36. }
Add Comment
Please, Sign In to add comment