public override bool Equals(object obj) { if (!base.Equals(obj)) { return(false);// check inherited fields first } CookedFood rhs = (CookedFood)obj; return(this._cookingMethod == rhs._cookingMethod);//check added fields }
static void Main() { //ReferenceEquality(); //StringEquality(); //DefaultStaticObjectBoolEqualsOfDotNet(); //ValueTypeEquality(); var apple = new Food("apple", FoodGroup.Fruit); var stewedApple = new CookedFood("stewed", "apple", FoodGroup.Fruit); var bakedApple = new CookedFood("baked", "apple", FoodGroup.Fruit); var stewedApple2 = new CookedFood("stewed", "apple", FoodGroup.Fruit); var apple2 = new Food("apple", FoodGroup.Fruit); DisplayWhetherEqualUsingEqualityOperator(apple, stewedApple); DisplayWhetherEqualUsingEqualityOperator(stewedApple, bakedApple); DisplayWhetherEqualUsingEqualityOperator(stewedApple, stewedApple2); DisplayWhetherEqualUsingEqualityOperator(apple, apple2); DisplayWhetherEqualUsingEqualityOperator(apple, apple); Console.WriteLine("Using class's equals method"); DisplayWhetherEqualUsingFoodsEquality(apple, stewedApple); DisplayWhetherEqualUsingFoodsEquality(stewedApple, bakedApple); DisplayWhetherEqualUsingFoodsEquality(stewedApple, stewedApple2); DisplayWhetherEqualUsingFoodsEquality(apple, apple2); DisplayWhetherEqualUsingFoodsEquality(apple, apple); Console.WriteLine("Using static Object.Equals"); DisplayWhetherEqualUsingStaticObjectEquality(apple, stewedApple); DisplayWhetherEqualUsingStaticObjectEquality(stewedApple, bakedApple); DisplayWhetherEqualUsingStaticObjectEquality(stewedApple, stewedApple2); DisplayWhetherEqualUsingStaticObjectEquality(apple, apple2); DisplayWhetherEqualUsingStaticObjectEquality(apple, apple); }
static void Main(string[] args) { string apple0 = "Apple"; string apple1 = "App" + "le"; Console.WriteLine("strings are {0} and {1}", apple0, apple1); Console.WriteLine(apple0 == apple1); Console.WriteLine(ReferenceEquals(apple0, apple1)); // return True because compiler will optimize memory and two var will refer to one string; string str1 = "erkl\u00e4ren"; //combining characters a + " string str2 = "erkla\u0308ren"; StringComparisons.DisplayAllComparisons(str1, str2); str1 = "Stra\u00dfe"; //expansion character, not depend on culture str2 = "Strasse"; StringComparisons.DisplayAllComparisons(str1, str2); Console.WriteLine("Current culture is " + Thread.CurrentThread.CurrentCulture); str1 = "apple"; str2 = "PINEAPPLE"; StringComparisons.DisplayAllComparisons(str1, str2); int result = string.Compare("lemon", "LEMON", StringComparison.OrdinalIgnoreCase); Console.WriteLine("Compare result is " + result); result = string.Compare("lemon", "LEMON", StringComparison.Ordinal); Console.WriteLine("Compare result is " + result); result = string.Compare("lemon", "lime", StringComparison.Ordinal); Console.WriteLine("Compare result is " + result); Console.WriteLine("********************************************"); CalorieCount cal300 = new CalorieCount(300); CalorieCount cal400 = new CalorieCount(400); DisplayOrder(cal300, cal400); DisplayOrder(cal400, cal300); DisplayOrder(cal300, cal300); if (cal300 < cal400) { Console.WriteLine("cal300 < cal400"); } if (cal300 == cal400) { Console.WriteLine("cal300 = cal400"); } Console.WriteLine("********************************************"); Food apple = new Food("apple", FoodGroup.Fruit); Food apple2 = new Food("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); Console.WriteLine(apple); Console.WriteLine(stewedApple); DisplayWhetherEqual(apple, stewedApple); DisplayWhetherEqual(stewedApple, bakedApple); DisplayWhetherEqual(stewedApple, stewedApple2); DisplayWhetherEqual(apple, apple2); DisplayWhetherEqual(apple, apple); Console.WriteLine("********************************************"); FoodItem bananai1 = new FoodItem("banana", FoodGroup.Fruit); FoodItem bananai2 = new FoodItem("banana", FoodGroup.Fruit); FoodItem chocolatei = new FoodItem("chocolate", FoodGroup.Sweets); Console.WriteLine("banana1 == banana2: " + (bananai1 == bananai2)); Console.WriteLine("banana2 == chocolate: " + (bananai2 == chocolatei)); Console.WriteLine("banana1 == chocolate: " + (bananai1 == chocolatei)); Console.WriteLine("********************************************"); str1 = "Click me"; str2 = string.Copy(str1); DiaplayWhwtherArgsEqual(str1, str2); Console.WriteLine("********************************************"); Tuple <int, int> tuple1 = Tuple.Create(1, 3); Tuple <int, int> tuple2 = Tuple.Create(1, 3); Console.WriteLine("Reference " + ReferenceEquals(tuple1, tuple2)); Console.WriteLine("operator " + (tuple1 == tuple2));// not overloaded for tuples = will compare references Console.WriteLine("method " + tuple1.Equals(tuple2)); Console.WriteLine("********************************************"); str1 = "Click me"; str2 = string.Copy(str1); Console.WriteLine("Reference " + ReferenceEquals(str1, str2)); Console.WriteLine("operator " + AreStringEqualOp(str1, str2)); Console.WriteLine("method " + AreStringEqualsMethod(str1, str2)); Console.WriteLine("********************************************"); Button button1 = new Button(); button1.Text = "Click me"; Button button2 = new Button(); button2.Text = "Click me"; Console.WriteLine("operator " + AreButtonEqualOp(button1, button2)); Console.WriteLine("method " + AreButtonEqualsMethod(button1, button2)); Console.WriteLine("********************************************"); Console.WriteLine("operator " + AreIntsEqualOp(3, 3)); Console.WriteLine("method " + AreIntsEqualsMethod(3, 3)); Console.WriteLine("********************************************"); Food banana = new Food("banana"); Food banana2 = new Food("banana"); Food chocolate = new Food("chocolate"); Console.WriteLine(banana.Equals(chocolate)); Console.WriteLine(banana.Equals(banana2)); // virtual object.Equals() Console.WriteLine(object.Equals(banana, banana2)); // static object.Equals() - if one of the objects is null Console.WriteLine(object.ReferenceEquals(banana, banana2)); // ALWAYS check reference to same instance Console.WriteLine("********************************************"); FoodStruct bananas = new FoodStruct("banana"); FoodStruct bananas2 = new FoodStruct("banana"); Console.WriteLine(bananas.Equals(bananas2)); //Console.WriteLine(bananas == bananas2);// operator == should be overloaded Console.ReadLine(); }