Confusing points of knowledge in C # (i)

Source: Internet
Author: User

The difference between = = and Equals ():

1. Example:

Code:

static void Main (string[] args)
{
Object m1 = 1;
Object m2 = 1;

Console.WriteLine (M1==M2);

Console.WriteLine (M1. Equals (m2));
Console.read ();
}

The code run results show:

Question: The same is the comparison of object objects, why is "= =" Comparison is false, and "Equals ()" Comparison is false

2. Explanation:

Why is this so for the result? This mainly involves the difference between = = and the Equals method, before we tell the difference between the two, we must first make clear that the--c# is equal: reference equality and value equality. An equality of values means that two objects protect the same value, for example, two integers with a value of 1 have value equality, and reference equality means that you are not comparing two objects, but are references to two objects, and both refer to the same object. To check for referential equality, you should use ReferenceEquals. To check for value equality, use Equals (for more information, refer to: http://msdn.microsoft.com/zh-cn/library/ms173147 (v=vs.90). aspx). Here's a look at their direct differences:

      • = = Compare the contents of the stack, for value types, "= =" compares the values of two objects, except that the string (the string type is a special case) other than the reference type is compared to two reference types within the stack address
      • The Equals method is a virtual method that is defined in object to compare whether the values of the referenced objects are equal. NET can override the Equals method, for example, the string type in. NET overrides the Equals method, which is used to compare whether the values of two strings are equal, not whether the string references are equal.

With the above theoretical basis, the following specific analysis of why the above program is the result:

      1. First, m1,m2 are reference types, and when performing m1==m2 operations, the comparison is between the value of M1 and M2 's address in the stack, that is, the reference is compared, because M1 and M2 point to the managed heap where 1 is a different address (which you can see in the debug state in the Memory window), So the result is, naturally, false.
      2. For M1. Equals (m2) compares the values referenced by M1 with M2, because they all reference 1 in the managed heap, their addresses are unequal, but the values are equal, all 1, and return to true.

Second, the difference between TypeOf and GetType ():

1. Example:

Code:

static void Main (string[] args)
{
Object m1 = 1;
Object m2 = 1;
ValueType is a reference type because it is a class, so return to False
Console.WriteLine (typeof (ValueType). Isvaluetype);
Console.WriteLine (M1. GetType (). Isvaluetype);
Console.read ();
}

The code runs to show the result:

2. Explanation:

To understand the above results, first we should understand the difference between TypeOf and GetType (I think the two are the same, this is a misunderstanding), the specific difference is:

        • typeof is an operator, and GetType () is a method
        • typeof gets the type of the System.Type object, GetType () obtains the current instance's type,
        • GetType () is a method of base class System.Object that can be called only after an instance has been established
        • typeof parameters can only be int, string, class, custom type, cannot be a specific instance, otherwise the compiler will error

Confusing points of knowledge in C # (i)

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.