internal static void DisplayWhetherEqual(Food food1, Food food2) { if (food1 == food2) Console.WriteLine(string.Format("{0,12} == {1}", food1, food2)); else Console.WriteLine(string.Format("{0,12} != {1}", food1, food2)); }
/// <summary> /// Implements reference types equality (classes) /// </summary> internal static void ReferenceTypesEquality() { Food apple = new Food("apple", FoodGroup.Fruits); CookedFood cookedApple = new CookedFood("apple", FoodGroup.Fruits, "stewed"); Food apple2 = new Food("apple", FoodGroup.Fruits); CookedFood cookedApple2 = new CookedFood("apple", FoodGroup.Fruits, "stewed"); DisplayWhetherEqual(apple, cookedApple); DisplayWhetherEqual(apple, apple2); DisplayWhetherEqual(cookedApple, cookedApple2); }
private static void SortAndShowArray(Food[] array) { Array.Sort(array, FoodNameComparer.Instance); foreach(var item in array) Console.WriteLine(item.ToString()); }
private static void SortingCustomCollection() { Console.WriteLine("First array of food"); Food[] food = new Food[]{ new Food("orange", FoodGroup.Fruits), new Food("banana", FoodGroup.Vegetables), new Food("banana", FoodGroup.Fruits) // the same names but in different group -> sorting by multiple fields }; SortAndShowArray(food); Console.WriteLine(); Console.WriteLine("Second array - mixed food and cooked food"); // There's no good solution for this sorting problem Food[] mixedFood = new Food[]{ new Food("apple", FoodGroup.Fruits), new CookedFood("apple", FoodGroup.Fruits, "baked") }; SortAndShowArray(mixedFood); Console.WriteLine(); Console.WriteLine("Third array - mixed food and cooked food - reversed order"); // There's no good solution for this sorting problem Food[] mixedFoodReversedOrder = new Food[]{ new CookedFood("apple", FoodGroup.Fruits, "baked"), new Food("apple", FoodGroup.Fruits) }; SortAndShowArray(mixedFoodReversedOrder); Console.WriteLine(); }