axeefectushka

Untitled

Nov 24th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         var a = new Interval(5, 6);
  9.         var b = new Interval(10, -8);
  10.         var c = new Interval(3, 2);
  11.         Interval e = null;
  12.         Console.WriteLine(b);
  13.         Console.WriteLine($"The hashcode of section ({c}) is {c.GetHashCode()}, ans the hashcode of section {a} is {a.GetHashCode()}. They are different despite their same lengths");
  14.         Console.WriteLine(a + c);
  15.         Console.WriteLine((uint)a);
  16.         Interval d = (5, 6);
  17.         Console.WriteLine(d.Equals(a));
  18.         Console.WriteLine(b.Lenght);
  19.         //Console.WriteLine((uint)e); -- ArgumentNullException()
  20.         Console.WriteLine(a + e);
  21.     }
  22. }
  23.  
  24.  
  25. public sealed class Interval
  26. {
  27.     public int Right { get; set; }
  28.  
  29.     public int Left { get; set; }
  30.  
  31.     public int Lenght => Math.Abs(Right - Left);
  32.  
  33.     public Interval(int a = 0, int b = 0)
  34.     {
  35.         if (a < Int32.MinValue || a > Int32.MaxValue || b < Int32.MinValue || b > Int32.MaxValue)
  36.             throw new ArgumentOutOfRangeException();
  37.  
  38.         if (a > b)
  39.         {
  40.             Right = a;
  41.             Left = b;
  42.         }
  43.         Right = b;
  44.         Left = a;
  45.     }
  46.  
  47.     public override bool Equals(object obj)
  48.     {
  49.         if (obj == null) return false;
  50.         Interval a = obj as Interval;
  51.         if (a as Interval == null) return false;
  52.         return Right == a.Right && Left == a.Left;
  53.     }
  54.  
  55.     public bool Equals(Interval obj)
  56.     {
  57.         if (obj == null) return false;
  58.         return Right == obj.Right && Left == obj.Left;
  59.     }
  60.  
  61.     public override int GetHashCode()
  62.     {
  63.         return Left << 2 + Right << 2 + Lenght << 16;
  64.     }
  65.  
  66.     public override string ToString()
  67.     {
  68.         return $"Left: {Left}, Right: {Right}, Lenght: {Lenght}";
  69.     }
  70.  
  71.     public static Interval operator +(Interval a, Interval b)
  72.     {
  73.         if (a == null) return b;
  74.         if (b == null) return a;
  75.  
  76.         return new Interval(a.Left + b.Left, a.Right + b.Right);
  77.     }
  78.  
  79.     public static explicit operator uint (Interval a)
  80.     {
  81.         if (a == null) throw new ArgumentNullException();
  82.  
  83.         return (uint)a.Lenght;
  84.     }
  85.  
  86.     public static implicit operator Interval(ValueTuple<int,int> a)
  87.     {
  88.         return new Interval(a.Item1, a.Item2);
  89.     }
  90. }
Add Comment
Please, Sign In to add comment