C # object comparison of C #

(1) : method of comparing objects

There are a lot of concepts. Just have a look:

1: ReferenceEquals:

Object.ReferenceEquals(left, right) static method: it can be seen from the name that it is used to compare whether they are the same reference. We should never override this method. It always returns false for the comparison of value type objects; The comparison between two null s always returns true. This method must be called using a class.

cat f1 = new cat(5);
cat f2 = new cat(5);
// ReferenceEquals
Console.WriteLine("ReferenceEquals:"+cat.ReferenceEquals(f1,f2));

2: Instance Equals

Equals() virtual version of system Object implementation code is also referenced. But because this method is virtual (virtual method), you can override it in your own class to compare objects by value. In particular, if you want an instance of a class to be used as a key in the dictionary, you need to override this method to compare values. Otherwise, override object Gethashcode(), the dictionary class containing objects either does not work or works very inefficiently. When overriding the equals () method, be careful that the overridden code does not throw exceptions.

The principles that should be followed in rewriting Equals: reflexivity, symmetry and transitivity. Namely: a=a; If a=b, then b=a; If a=b, b=c, then a=c; The other two objects are either equal or unequal, so the method should not throw an exception.

cat f1 = new cat(5);
cat f2 = new cat(5);
// object.Equals
Console.WriteLine("Equals:" + cat.Equals(f1, f2));

3: Static Equals

Object.Equals(left, right) static method: this method will never need to be rewritten, because it will eventually give the judgment to the instance Equls method of the parameter left. Therefore, it is not necessary to rewrite this method, just ensure that the instance equals returns the desired result.

cat f1 = new cat(5);
cat f2 = new cat(5);
// Equals
Console.WriteLine("Equals:" + f1.Equals(f2));

4: Equality operator==

Comparison operator = =: for reference types, the default is the source of the comparison reference (except System.String). For value types, the default comparison value. For custom structures, if the overloaded operator method is not displayed, it cannot be used.

Since the comparison operators are required to be overloaded in pairs in C #, the overloaded = = operator must also be overloaded= Operator, otherwise compilation errors will also occur.

If overloaded, the operators' ', "! =" and the equals method and GetHashCode method should be overloaded at the same time, because they should maintain the same equality logic. But do not call Equals again. It is best to call = = in Equals.

cat f1 = new cat(5);
cat f2 = new cat(5);
// ==
Console.WriteLine("==:" + (f1 == f2));
 
static void Main(string[] args)
        {
            cat f1 = new cat(5);
            cat f2 = new cat(5);
            // ReferenceEquals
            Console.WriteLine("ReferenceEquals:"+cat.ReferenceEquals(f1,f2));
            // object.Equals
            Console.WriteLine("Equals:" + cat.Equals(f1, f2));
            // Equals
            Console.WriteLine("Equals:" + f1.Equals(f2));
            // ==
            Console.WriteLine("==:" + (f1 == f2));
        }

The above code output is false: because f1 is a memory stack and f2 is a memory stack. So they don't want to wait

To make the above equations equal.

static void Main(string[] args)
        {
            cat f1 = new cat(5);
            cat f2 = f1;
            // ReferenceEquals
            Console.WriteLine("ReferenceEquals:"+cat.ReferenceEquals(f1,f2));
            // object.Equals
            Console.WriteLine("Equals:" + cat.Equals(f1, f2));
            // Equals
            Console.WriteLine("Equals:" + f1.Equals(f2));
            // ==
            Console.WriteLine("==:" + (f1 == f2));
        }

In this way, all the above outputs are true.

(2) : override instance Equals method:

override is used in the class, and the return value type is Boolean.

// After rewriting, Equals compares the value of A. if the value of a is the same, it is true
public override bool Equals(object obj)
{
    cat f3 = (cat)obj;
    if(f3.a == this.a)
    {
          return true;
       }else{
          return false;
       }
}

From the above example, we can see that after rewriting, Equals compares the value of a in the object. If a is equal, it is true.

Moreover, we can also see that if the instance Equals method is rewritten, the static Equals method will also change.

If you have good suggestions, please enter your comments below.

Welcome to personal blog
https://guanchao.site

Welcome to the applet:

Keywords: Javascript C# Back-end Mini Program

Added by glima on Fri, 04 Mar 2022 06:27:11 +0200