Consider that fact that object. Equals look for value as well as reference. However you can overload it to just check for value matching by overriding Equals method in class.
This is the application of "EQUALS" in various scenario.More to come,
-
- using System;
- class Product {
- public int Price {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- }
- class ProductWithOverride {
- public int Price {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public override bool Equals(object obj) {
- if (obj == null) return false;
- if (this.GetType() != obj.GetType()) return false;
- ProductWithOverride p = (ProductWithOverride) obj;
- return (this.Price == p.Price) && (this.Name == p.Name);
- }
- }
- class Demo {
- static void Main() {
- Product p1 = new Product {
- Price = 10, Name = "foo"
- };
- Product p2 = new Product {
- Price = 10, Name = "foo"
- };
- bool a = p1.Equals(p2);
- Console.WriteLine(a.ToString());
-
- p2 = p1;
- bool b = p1.Equals(p2);
- Console.WriteLine(b.ToString());
-
-
- ProductWithOverride po1 = new ProductWithOverride {
- Price = 10, Name = "foo"
- };
- ProductWithOverride po2 = new ProductWithOverride {
- Price = 10, Name = "foo"
- };
- bool val = po1.Equals(po2);
- Console.WriteLine(val.ToString());
-
- po2 = po1;
- bool val1 = po1.Equals(po2);
- Console.WriteLine(val1.ToString());
-
-
- string test1 = "testme";
- string test2 = "testme";
- bool stringval = test1.Equals(test2);
- Console.WriteLine(stringval.ToString());
- string test3 = "testmetoo";
- string test4 = "testmetoo";
- test4 = test3;
- bool stringval1 = test3.Equals(test4);
- Console.WriteLine(stringval1.ToString());
-
-
-
-
-
- Product p3 = new Product {
- Price = 10, Name = "foo"
- };
- Product p4 = new Product {
- Price = 10, Name = "foo"
- };
- Console.WriteLine("person1a and person1b: {0}", ((object) p3).Equals((object) p4));
- p4 = p3;
- Console.WriteLine("person1a and person1b: {0}", ((object) p3).Equals((object) p4));
-
- ProductWithOverride po3 = new ProductWithOverride {
- Price = 10, Name = "foo"
- };
- ProductWithOverride po4 = new ProductWithOverride {
- Price = 10, Name = "foo"
- };
- Console.WriteLine("person1a and person1b: {0}", ((object) po3).Equals((object) po4));
- po4 = po3;
- Console.WriteLine("person1a and person1b: {0}", ((object) po3).Equals((object) po4));
-
-
- string test1 = "testme";
- string test2 = "testme";
- object one = test1;
- object two = test2;
- object three = null;
- object four = null;
- Console.WriteLine("one and two: {0}", (one.Equals(two)));
- two = one;
- Console.WriteLine("one and two: {0}", (one.Equals(two)));
- Console.WriteLine("three and four: {0}", (three.Equals(four)));
-
-
- }
- }
Use of "== " on reference type: - Note- For reference types where == has not been overloaded,
- It compares whether two references refer to the same object - which is exactly what the implementation of Equals does in System. Object.
- Also to note, Operators are overloaded, not overridden,
- For predefined value types,
- The equality operator (==) returns true if the values of its operands are equal, false otherwise.
- For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.
- using System;
- class Product {
- public int Price {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- }
- class ProductWithOverride {
- public int Price {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
-
-
- public static bool operator == (ProductWithOverride a, ProductWithOverride b) {
-
- if (System.Object.ReferenceEquals(a, b)) {
- return true;
- }
-
-
- if (((object) a == null) || ((object) b == null)) {
- return false;
- }
-
-
- return a.Name == b.Name && a.Price == b.Price;
- }
- public static bool operator != (ProductWithOverride a, ProductWithOverride b) {
- return !(a == b);
- }
-
-
- }
- class Demo {
- static void Main() {
- Product p1 = new Product {
- Price = 10,
- Name = "foo"
- };
- Product p2 = new Product {
- Price = 10,
- Name = "foo"
- };
- bool a = (p1 == p2);
- Console.WriteLine(a.ToString());
-
- p2 = p1;
- bool b = (p1 == p2);
- Console.WriteLine(b.ToString());
-
-
- ProductWithOverride po1 = new ProductWithOverride {
- Price = 10,
- Name = "foo"
- };
- ProductWithOverride po2 = new ProductWithOverride {
- Price = 10,
- Name = "foo"
- };
- bool val = (po1 == po2);
- Console.WriteLine(val.ToString());
-
- po2 = po1;
- bool val1 = (po1 == po2);
- Console.WriteLine(val1.ToString());
-
-
- }
- }
For value type - Both operator checks for value Equality.