static void Main(string[] args) { Food teste = new Food("teste"); Food teste2 = new Food("teste"); Food choc = new Food("choc"); Console.WriteLine(teste.Equals(choc)); Console.WriteLine(teste.Equals(teste2)); Console.WriteLine(teste); Console.WriteLine(teste2); }
static void Main(string[] args) { var strBanana = "banana"; var banana = new Food(strBanana); var banana2 = new Food(strBanana); var chocolate = new Food("chocolate"); Console.WriteLine("banana.Equals(chocolate)? " + banana.Equals(chocolate)); Console.WriteLine("banana.Equals(banana2)? " + banana.Equals(banana2)); var strBanana2 = string.Copy(strBanana); Console.WriteLine("\nString is a reference type, but .equals is overriden by .Net to compare values. Same for Tuples and delegates"); Console.WriteLine("strBanana.Equals((object)strBanana2)? " + strBanana.Equals((object)strBanana2)); var strTent = "Tent"; var tent = new Structure(strTent); var tent2 = new Structure(strTent); Console.WriteLine("\nStruct is a value type, so .equals will compare values. But because it uses reflection to find fields to compare by, performance is usually poor"); Console.WriteLine("tent.Equals(tent2)? " + tent.Equals(tent2)); Console.WriteLine("\nVirtual .Equals() vs Static .Equals()"); Console.WriteLine("banana.Equals(null)? " + banana.Equals(null)); try{ Food n = null; Console.WriteLine("null.Equals(banana)? " + n.Equals(banana)); }catch (NullReferenceException) { Console.WriteLine("null.Equals(banana) threw an NPE"); } Console.WriteLine("object.Equals(banana, null)? " + object.Equals(banana, null)); Console.WriteLine("object.Equals(null, banana)? " + object.Equals(null, banana)); Console.WriteLine("object.Equals(null, null)? " + object.Equals(null, null)); Console.WriteLine("\nobject.ReferenceEquals() (there is no ability to override this method)"); Console.WriteLine("strBanana.Equals((object)strBanana2)? " + strBanana.Equals((object)strBanana2)); Console.WriteLine("BUT"); Console.WriteLine("object.ReferenceEquals(strBanana, strBanana2)? " + object.ReferenceEquals(strBanana, strBanana2)); }
public static void Run() { //FoodEquals: Demonstrates Equals() giving reference equality for food - turns to value equality if Food is a struct Console.WriteLine("----------------------<<<FoodEquals>>>------------------------------"); Food banana = new Food("banana"); Food banana2 = new Food("banana"); Food chocolate = new Food("chocolate"); // behaviour for non-null Console.WriteLine(banana.Equals(chocolate)); //false Console.WriteLine(banana.Equals(banana2)); ///////TRUE IF STRUCT, FALSE FORT CLASS // behaviour for nulls Console.WriteLine(banana.Equals(null)); //false Console.WriteLine(object.Equals(banana, null)); //false Console.WriteLine(object.Equals(null, banana)); //false Console.WriteLine(object.Equals(null, null)); //true StringRun(); //EquatableInt: Demonstrates calling Equals() for ints Console.WriteLine("-----------------------------------------------------------------\r\n\r\n\r\n"); Console.WriteLine("----------------------<<<EquatableInt>>>------------------------------"); int three = 3; int threeAgain = 3; int four = 4; Console.WriteLine(three.Equals(threeAgain)); //true Console.WriteLine(three.Equals(four)); //false //FoodEqualsButton: Demonstrates the lack of type safety in object.Equals() Console.WriteLine("-----------------------------------------------------------------\r\n\r\n\r\n"); Console.WriteLine("----------------------<<<FoodEqualsButton>>>------------------------------"); Food food = new Food("chocolate"); Button button = new Button(); button.Text = "I'm a chocolate button - click me!"; Console.WriteLine(food.Equals(button)); //false }
public static void Run() { //EqualityInt: Demonstrates == and Equals() for integers Console.WriteLine("----------------------<<<EqualityInt>>>------------------------------"); Console.WriteLine("Operator: " + AreIntsEqualOp(3, 3)); //true Console.WriteLine("Method: " + AreIntsEqualMethod(3, 3)); //true //EqualButtons: Demonstrates == and Equals() for buttons Console.WriteLine("-----------------------------------------------------------------\r\n\r\n\r\n"); Console.WriteLine("----------------------<<<EqualButtons>>>------------------------------"); Button button1 = new Button(); button1.Text = "Click me now!"; Button button2 = new Button(); button2.Text = "Click me now!"; Console.WriteLine("Operator: " + AreButtonsEqualOp(button1, button2)); //false Console.WriteLine("Method: " + AreButtonsEqualMethod(button1, button2)); //false //EqualStrings: Demonstrates == and Equals() for strings Console.WriteLine("-----------------------------------------------------------------\r\n\r\n\r\n"); Console.WriteLine("----------------------<<<EqualStrings>>>------------------------------"); string str1 = "Click me now!"; string str2 = string.Copy(str1); Console.WriteLine("Reference: " + ReferenceEquals(str1, str2)); //false Console.WriteLine("Operator: " + AreStringsEqualOp(str1, str2)); //true - overloaded Console.WriteLine("Method: " + AreStringsEqualMethod(str1, str2)); //true - overloaded //EqualValueTypes: Demonstrates == doesn't work for nonprimitive value types out of the box Console.WriteLine("-----------------------------------------------------------------\r\n\r\n\r\n"); Console.WriteLine("----------------------<<<EqualValueTypes>>>------------------------------"); Food banana = new Food("banana"); Food banana2 = new Food("banana"); Food chocolate = new Food("chocolate"); Console.WriteLine(banana.Equals(chocolate)); //false Console.WriteLine(banana.Equals(banana2)); //false // this line won't compile because no == overload has been provide for Food, and Food is a valuetype Console.WriteLine(banana == chocolate); //false //EqualTuples: Demonstrates that == and object.Equals() are inconsistent for tuples Console.WriteLine("-----------------------------------------------------------------\r\n\r\n\r\n"); Console.WriteLine("----------------------<<<EqualTuples>>>------------------------------"); Tuple <int, int> tuple1 = Tuple.Create(1, 3); // the same Tuple<int, int> tuple3 = new Tuple<int, int>(1,3); Tuple <int, int> tuple2 = Tuple.Create(1, 3); Console.WriteLine("Reference: " + ReferenceEquals(tuple1, tuple2)); //false Console.WriteLine("Method: " + tuple1.Equals(tuple2)); //true Console.WriteLine("Operator: " + (tuple1 == tuple2)); //false - not overloaded //Inheritance: Demonstrates that == doesn't work well with inheritance Console.WriteLine("-----------------------------------------------------------------\r\n\r\n\r\n"); Console.WriteLine("----------------------<<<Inheritance>>>------------------------------"); object str1Inh = "apple"; object str2Inh = string.Copy((string)str1); //string.Copy(str1) - the same // The == operator will give the wrong result because it is not virtual // Equals() methods will work OK Console.WriteLine("Reference : " + ReferenceEquals(str1Inh, str2Inh)); //false Console.WriteLine("Method : " + str1.Equals(str2Inh)); //true Console.WriteLine("Operator : " + (str1 == str2Inh)); //false Console.WriteLine("Static : " + object.Equals(str1Inh, str2Inh)); //false Console.WriteLine("mine : " + string.Equals(str1Inh, str2Inh)); //false == object.Equals(str1Inh, str2Inh)) will call //Generics: Demonstrates that you need to use object.Equals() with generics Console.WriteLine("-----------------------------------------------------------------\r\n\r\n\r\n"); Console.WriteLine("----------------------<<<Generics>>>------------------------------"); string str1Gen = "apple"; string str2Gen = string.Copy(str1Gen); DisplayWhetherArgsEqual(str1Gen, str2Gen); }