[C # Basic Knowledge series] C # confusing knowledge points

Source: Internet
Author: User

I. Introduction

Today in the Forum saw a friend raised such a problem, the problem is roughly (The problem link is: http://social.msdn.microsoft.com/Forums/zh-CN/52e6c11f-ad28-4633-a434-fc4d09f4d23d) is like this:

    Main( m1 = m2 = ==

Do not run this code in Visual Studio. First, let's guess what the running result of this code is, if the result you guessed is exactly the same as the result you run and you know the reason, then there is no need to read the following content in this article, if you do not understand the running results, continue with the analysis of the following content. I believe that you can definitely solve your doubts.

Ii. Difference Between = and Equals

The running result of the above problem is:

Why is the result like this? This mainly involves the difference between the = and Equals methods. before talking about the difference between the two, we must first make it clear that there are two differences in C #: The reference is equal and the value is equal. Equal values mean that two objects protect the same value. For example, two integers with a value of 1 have equal values. Equal references mean that not two objects are compared, but two objects are referenced, the two references the same object. To check the equality of reference, use ReferenceEquals. To check for equality of value, use Equals (For details, refer to: http://msdn.microsoft.com/zh-cn/library/ms173147 (v = vs.90). aspx ). Let's take a look at their direct differences:

  • = Compare the content in the stack. For the value type, "=" compares the values of two objects,String (the string type is a special case)Other reference types are compared with the addresses of two reference types in the stack.
  • The Equals method is a virtual method defined in an Object to compare the values of the referenced objects ,. the Equals method can be rewritten for all types in. NET. For example, in. the Equals method is rewritten for the string type in. NET to compare whether the values of the two strings are equal, rather than whether the string references are equal.

With the above theoretical basis, we will analyze the reason why the above program is as follows:

Main (str1 = str2 = str3 = ([] {, str4 = ([] {, + (str1 = ++ (str3 = +View Code

The running result is:

 

Iii. Differences between typeof and GetType

From the above question, I again contacted the difference between typeof and GetType, so here I will sum up them together. First, I will introduce the differences between them by a program:

Main (m1 = m2 = // ValueType is the reference type. Because it is a class, false Console. WriteLine (

To understand the above running results, we should first understand the difference between typeof and GetType (previously I thought the two are the same, which is a misunderstanding). The specific difference is:

  • Typeof is an operator, while GetType () is a method.
  • TypeofSystem. TypeObject, Type obtained by GetType,
  • GetType () is the base class System. Object method. It can be called only after an instance is created.
  • Typeof can only be int, string, class, custom type, cannot be

 

Iv. Summary

This article mainly records what you learned when answering questions, and hopes to help those who have the same questions.

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.