Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main()
- {
- var a = new Interval(5, 6);
- var b = new Interval(10, -8);
- var c = new Interval(3, 2);
- Interval e = null;
- Console.WriteLine(b);
- 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");
- Console.WriteLine(a + c);
- Console.WriteLine((uint)a);
- Interval d = (5, 6);
- Console.WriteLine(d.Equals(a));
- Console.WriteLine(b.Lenght);
- //Console.WriteLine((uint)e); -- ArgumentNullException()
- Console.WriteLine(a + e);
- }
- }
- public sealed class Interval
- {
- public int Right { get; set; }
- public int Left { get; set; }
- public int Lenght => Math.Abs(Right - Left);
- public Interval(int a = 0, int b = 0)
- {
- if (a < Int32.MinValue || a > Int32.MaxValue || b < Int32.MinValue || b > Int32.MaxValue)
- throw new ArgumentOutOfRangeException();
- if (a > b)
- {
- Right = a;
- Left = b;
- }
- Right = b;
- Left = a;
- }
- public override bool Equals(object obj)
- {
- if (obj == null) return false;
- Interval a = obj as Interval;
- if (a as Interval == null) return false;
- return Right == a.Right && Left == a.Left;
- }
- public bool Equals(Interval obj)
- {
- if (obj == null) return false;
- return Right == obj.Right && Left == obj.Left;
- }
- public override int GetHashCode()
- {
- return Left << 2 + Right << 2 + Lenght << 16;
- }
- public override string ToString()
- {
- return $"Left: {Left}, Right: {Right}, Lenght: {Lenght}";
- }
- public static Interval operator +(Interval a, Interval b)
- {
- if (a == null) return b;
- if (b == null) return a;
- return new Interval(a.Left + b.Left, a.Right + b.Right);
- }
- public static explicit operator uint (Interval a)
- {
- if (a == null) throw new ArgumentNullException();
- return (uint)a.Lenght;
- }
- public static implicit operator Interval(ValueTuple<int,int> a)
- {
- return new Interval(a.Item1, a.Item2);
- }
- }
Add Comment
Please, Sign In to add comment