The difference between equals and = = in C #

Source: Internet
Author: User

This question heard is a big company interview will ask questions, didn't know, curiosity seduce me to study

First, from the value type analysis, first write a few simple code for testing, two lines of statement output is true,

The Description = = is the same as the Equals function and the values are judged.

Enter int inside to view under

overriding Equals (Object obj)

overloaded equals (int obj)

You can see int. equals is compared to the target value itself, and = = is the same function.

To determine if the other value type equlas and = = are the same, select a float type to view

Next start analysis of all types of base class object, also write a few simple code to test, two line statement output is False

Description = = is the same as the Equals function, and the reference address is judged

Also enter object inside to view under

Continue into RuntimeHelpers to find equals

Find there is no code here, click on the left button to return to object. Equals

Then parse the string in the reference type, also write a few simple code to test, two lines of statement output is true

That's interesting, why is the output true?

According to conventional understanding,

Use the New keyword in the managed heap to request memory to store string type data, and then return the memory address assignment to

Variable to point to a string type object in the managed heap

Each instantiated object is not the same address in the managed heap.

With a question or into a string inside to do a probing

You can see that the operator = = is overloaded internally, = = and equals are the same functions

Continue to view equals

Into the Equalshelper, a long, great segment.

1       Private unsafe Static BOOLequalshelper (String stra, string StrB)2         {3Contract.Requires (Stra! =NULL);4Contract.Requires (StrB! =NULL);5Contract.Requires (Stra.length = =strb.length);6             //Save current string length7             intLength =stra.length;8             //fixed the first character address of two strings9             fixed(Char* ap = &stra.m_firstchar)fixed(Char* BP = &Strb.m_firstchar)Ten             { One                 //defines two pointers to hold the first address of the string A                 Char* A =ap; -                 Char* B =BP; -                 //below to determine whether it is a AMD64-bit system, I am not going down to execute the                 //Unroll the Loop - #ifAMD64 -                 //For AMD64 bit platform we unroll by -                 //Check 3 Qword at a time. This is the less code +                 //than the-bit case and is shorter -                 //pathlength +  A                  while(Length >= A) at                 { -                     if(*(Long*) A! = * (Long*) b)return false; -                     if(*(Long*) (A +4) != *(Long*) (b +4))return false; -                     if(*(Long*) (A +8) != *(Long*) (b +8))return false; -A + = A; B + = A; Length-= A; -                 } in                  - #else            to                 //a paragraph feeling is superfluous, directly executes the B paragraph to be finished +                 //determines whether the string a is longer than 10 and is true to compare four bytes per read -                 //if it is not the same, return false directly, the last pointer moves backwards by 10 bytes, the length is reduced by ten the                  while(Length >=Ten) *                 { $                     if(*(int*) A! = * (int*) b)return false;Panax Notoginseng                     if(*(int*) (A +2) != *(int*) (b +2))return false; -                     if(*(int*) (A +4) != *(int*) (b +4))return false; the                     if(*(int*) (A +6) != *(int*) (b +6))return false; +                     if(*(int*) (A +8) != *(int*) (b +8))return false; AA + =Ten; B + =Ten; Length-=Ten; the                 } + #endif -  $                 //This depends in the fact that the String objects is $                 //Always zero terminated and, the terminating zero is not included -                 //In the length. For odd string sizes, the last compare would include -                 //The zero Terminator. the  -                 //Segment BWuyi                 //here is the first 10 bytes matching case, starting from +10 open position to continue to compare every four bytes the                 //as long as the mismatch, jumping out of the loop, that length is certainly not 0, where break is also redundant, direct return false -                 //if all the bytes match, the final length is 0, the loop jumps out, and the true Wu                  while(Length >0)  -                 { About                     if(*(int*) A! = * (int*) b) Break; $A + =2; B + =2; Length-=2; -                 } -  -                 return(Length <=0); A             } +}

After a long analysis, we can conclude that = = compares each character with equals, not the reference address of the comparison.

Try a custom reference type and create a class. Write a few simple code for testing, two lines of statement output is False

Description Custom Reference type = = is the same as equals, the reference address is compared

The conclusion is that the results are the same, whether = = or equals.

When a type is a value type, the comparison is numeric

When a type is a reference type, the reference address is compared, except that the string type compares the contents of the reference address

The difference between equals and = = in C #

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.