The = = operator Determines whether two values in the stack are equal

Source: Internet
Author: User

By default (that is, if the type does not overload the = = operator), the = = operator Determines whether two values in the stack are equal. To verify this conclusion, consider the following procedure:

Namespace Judgeequals
{
Class Program
{
static void Main (string[] args)
{
int a = 10;
int B = 10;
Console.WriteLine (A==B);

string S1 = "abc";
string s2 = "abc";
Console.WriteLine (S1==S2);

person P1 = new person ();
person P2 = new person ();
Console.WriteLine (P1==P2);
Console.readkey ();

}
}

public class Person
{
public string Name {get; set;}
public int Age {get; set;}
public string Email {get; set;}
}
}

The results of the above program operation are as follows:


Console.readkey () in the main method, add a breakpoint to the line, and then press to debug. Then enter &A,&B,&S1,&S2,&P1,&P2 in the Immediate window to get the following information:

It can be concluded that the = = operator is to determine whether two values in the stack are equal. But you may have another question, which is why S1 and S2 have the same two storage addresses? The simple point is that the CLR maintains a string constant resident pool, and the CLR will go to the resident pool to query whether the string constant exists before creating a new string constant. Returns the corresponding address if it exists, or creates a new string constant and returns it.

The = = operator Determines whether two values in the stack are equal

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.