Advertisement
Guest User

Untitled

a guest
Aug 14th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ExperimentProject
  4. {
  5.     public class Program
  6.     {
  7.         public abstract class MyId
  8.         {
  9.             public abstract int Value { get; set; }
  10.  
  11.             public static bool operator ==(MyId a, MyId b)
  12.             {
  13.                 if (a.GetType() != b.GetType())
  14.                     return false;
  15.                 if (a.Value == b.Value)
  16.                     return true;
  17.                 return false;
  18.             }
  19.  
  20.             public static bool operator !=(MyId a, MyId b)
  21.             {
  22.                 return !(a == b);
  23.             }
  24.         }
  25.  
  26.         public class MyId1 : MyId
  27.         {
  28.             public MyId1(int id)
  29.             {
  30.                 Value = id;
  31.             }
  32.  
  33.             public override int Value { get; set; }
  34.         }
  35.  
  36.         public class MyId2 : MyId
  37.         {
  38.             public MyId2(int id)
  39.             {
  40.                 Value = id;
  41.             }
  42.  
  43.             public override int Value { get; set; }
  44.         }
  45.  
  46.         public static void Main()
  47.         {
  48.             var q1 = new MyId1(2);
  49.             var q2 = new MyId1(2);
  50.             var q3 = new MyId2(2);
  51.  
  52.             bool f1 = q1 == q2;
  53.             bool f2 = q1 == q3;
  54.  
  55.             Console.WriteLine(f1); //true
  56.             Console.WriteLine(f2); //false
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement