예제 #1
0
 // helper function to check equality
 static void DisplayWhetherEqual(FoodBaseRefType food1, FoodBaseRefType food2)
 {
     if (food1 == food2)
     {
         Console.WriteLine(string.Format($"{food1} == {food2}"));
     }
     else
     {
         Console.WriteLine(string.Format($"{food1} != {food2}"));
     }
 }
예제 #2
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (ReferenceEquals(obj, this))
            {
                return(true);
            }
            if (obj.GetType() != this.GetType())
            {
                return(false);
            }

            FoodBaseRefType rhs = obj as FoodBaseRefType;

            return(this._name == rhs._name && this._group == rhs._group);
        }
예제 #3
0
        public static void RunDemo()
        {
            Console.WriteLine("RefTypeEquality demo: Demonstrates two reference types- \n" +
                              "FoodBase(base type) and CookedFood (sealed derived type)\n" +
                              "for which equality has been implemented correctly\n");

            FoodBaseRefType apple        = new FoodBaseRefType("apple", FoodGroup.Fruit);
            CookedFood      stewedApple  = new CookedFood("stewed", "apple", FoodGroup.Fruit);
            CookedFood      bakedApple   = new CookedFood("baked", "apple", FoodGroup.Fruit);
            CookedFood      stewedApple2 = new CookedFood("stewed", "apple", FoodGroup.Fruit);
            FoodBaseRefType apple2       = new FoodBaseRefType("apple", FoodGroup.Fruit);

            DisplayWhetherEqual(apple, stewedApple);
            DisplayWhetherEqual(stewedApple, bakedApple);
            DisplayWhetherEqual(stewedApple, stewedApple2);
            DisplayWhetherEqual(apple, apple2);
            DisplayWhetherEqual(apple, apple);

            Console.WriteLine();
        }