Analysis of C # judgment Functions

Source: Internet
Author: User

. Net has four C # judgment functions? Many people may doubt this title. In fact, this is the case.. Net provides ReferenceEquals, static Equals, specific Equals, = operator, and other functions. However, there is a slight relationship between the four functions. Changing the implementation of one function affects the operation results of other functions.

The first thing we need to talk about is the Object. ReferenceEquals and Object. Equals static functions. For them, they do not need to be rewritten because they have completed the operations they need to do. For the static function Object. ReferenceEquals, the function situation is as follows:

 
 
  1. public static bool ReferenceEquals object left, object right ); 

This function is used to determine whether two referenced objects point to the same address. With this description, the scope of use is determined, that is, you can only operate on the reference type. For any value type data operation, even if it is identified by itself, false is returned. this is mainly because the value type data needs to be boxed when this function is called, that is, for the following form.

 
 
  1. int n = 10;  
  2. Object.ReferenceEquals n, n ); 

This is because the data of n is packed twice, and the address after each packing is different, the result of Object. ReferenceEquals n, n is always false.

For the first C # judgment function, there is no good extension, because it has done well.

For the static function of the second Object. Equals, the form is as follows:

 
 
  1. public static bool Equals object left, object right ); 

According to the analysis in the book, the approximate function code is as follows:

 
 
  1. public static void Equals object left, object right )  
  2. {  
  3. // Check object identity  
  4. if left == right )  
  5. return true;  
  6. // both null references handled above  
  7. if  left == null ) ||  right == null ) )  
  8. return false;  
  9. return left.Equals right );  
  1. C # Implementation of message sending between applications
  2. Introduction to C # Time
  3. Analysis on the technical features of C # interfaces and abstract classes
  4. A c # Time computing instance
  5. C # detailed inheritance knowledge

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.