Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main()
- {
- Color red = new Color(255, 0, 0);
- Color red1 = new Color(255, 0, 0);
- Color green = new Color(0, 255, 0);
- Color unknownColor = new Color(250, 2, 3);
- Console.WriteLine(red.ToString());
- Console.WriteLine(unknownColor.ToString());
- Object obj = new Object();
- Console.WriteLine(red.Equals(null));
- Console.WriteLine(red.Equals(obj));
- Console.WriteLine(red.Equals(green));
- Console.WriteLine(red.Equals(red1));
- Console.WriteLine($"Red's hashcode: {red.GetHashCode()}, Green's hashcode: {green.GetHashCode()}, unknownColor's hashcode: {unknownColor.GetHashCode()}");
- Console.ReadKey();
- }
- }
- class Color
- {
- public byte R { get; }
- public byte G { get; }
- public byte B { get; }
- public Color(byte r, byte g, byte b)
- {
- R = r;
- G = g;
- B = b;
- }
- public override string ToString()
- {
- if (R == 255 && G == 0 && B == 0) return "Red";
- if (R == 0 && G == 255 && B == 0) return "Green";
- if (R == 0 && G == 0 && B == 255) return "Blue";
- else
- {
- return $"R:{R}, G:{G}, B:{B}";
- }
- }
- public override bool Equals(object obj)
- {
- if (obj == null) return false;
- if (!(obj is Color color)) return false;
- return color.R == R && color.G == G && color.B == B;
- }
- public bool Equals(Color color)
- {
- if (color == null) return false;
- return (color.R == R && color.G == G && color.B == B);
- }
- public override int GetHashCode() => (R << 2 + G << 2)<<1 + B << 2;
- }
Add Comment
Please, Sign In to add comment