What is the difference between = = and Equals () in C #

Source: Internet
Author: User
such as the following code:

int age = 25; Short newage = 25; Console.WriteLine (age = = newage);  True Console.WriteLine (Newage.equals (age)); False Console.ReadLine ();

int and short are primitive types, but the comparison with "= =" returns True,equals () to return false. Why is it?

Answers:

Nutshell:

"Equals ()" is more complex than "= =".

Specifically:

An object of the original type override (override) base class. Equals (object), and when object in parentheses returns true with its type and value (note that the nullable type is also suitable for the above judgment; the non-null nullable type is always boxed into an instance of the underlying type).

Because NewAge is short, newage.equals (object) returns True when the object is short and the value is equal to the NewAge value. You are passing an int object, so it returns false.

By contrast, the "= =" operator is defined as an operation with two shaping (int) or two short integer (shorter) or two long shape (long). When the "= =" Two parameter one is shaped and a short integer, the compiler implicitly converts the shorter to int, and compares the converted int value size.

Other ways to make it work:

The original type also has its own equals () method, and equals accepts parameters of the same type.

If you write age. Equals (NewAge), the compiler will select Int. Equals (int) acts as the best overloaded (overload) method and implicitly converts short to int. It then returns true because this method directly compares the size of the two int values.

Short also has a short. Equals (short) method, but an int type cannot be implicitly converted to short, so it is not called.

You can force this method to be called using the cast transformation:

Console.WriteLine (Newage.equals (short)); True

This will call short directly. Equals (short), no boxing operation. If age is greater than 32767, it throws an overflow exception.

You can also call short. This overload of Equals (object), but requires explicitly passing an object that is boxed with the same type:

Console.WriteLine (Newage.equals ((object) (short)); True

Like the previous optional method (short. Equals (short), if the size exceeds the short range, it also throws an overflow exception. Unlike previous solutions, it boxed short into a object--wasted time and memory.

Source Code:

Here is the actual use of equals ():

public override bool Equals (Object obj) {        if (!) ( obj is Int16)) {            return false;        }        return m_value = = ((Int16) obj). m_value;    }    public bool Equals (Int16 obj)    {        return m_value = = obj;    }



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.