. Net object determination (2)

Source: Internet
Author: User

. Net object judgment, coupled together, I try not to involve other judgment methods in any sentence. However, instace. Equals still does not use operator =, so I use operator = as the third part.

Iii. Operator = in-depth nature

By default, operator = determines whether the specified variable is the same as object. referenceequals when determining the nature of the object of the reference type is the same as that of object. referenceequals.

Of course, C #. Net can define opeartor =. The public thinks that you should avoid customizing operator =. Any definition will confuse the user's role in opeartor =. After all, no logic is more logical than a =.

If you want to use operator = for user-defined value types, you need to determine opeartor = and opeartor! = Are defined separately. Most value types of FCL do this, such as datetime and decimal.

In this case, the int32, int64, and byte equivalence types are not defined and opeartor = is not defined. However, the compiler still supports the above types of variables using operator = for determination. For more information, see byte, shrot, Int, long, Char, float, double, bool, decimal, String, object, and dynamic. The data types directly supported by the compiler are called primitive types. The primitive type is directly mapped to the type existing in the framework class library (FCL. For example, in C #, Int Is directly mapped to system. int32.

When two int type variables are judged, the two values are first pushed into the computing stack, and the CLR uses the CEQ command to directly judge the values. Of course, the primitive type supports defining opeartor. For example, decimal defines opeartor =.

Interestingly, sbyte, ushort, uint, and ulong are not primitive types and opeartor is not defined. However, the above types directly use opeartor =, etc. The CSC compiler can still compile and pass. Take ushort as an example. Please take a look at the Code:

1 static void Main()2 { 3       ushort u1=1;4       ushort u2 = 2;5       var b = u1 == u2;6  }

The IL code obtained through ildasm Decompilation

1. method private hidebysig static void main () cel managed 2 {3. entrypoint 4 // code size 11 (0xb) 5. maxstack 2 6. locals Init (uint16 V_0, 7 uint16 v_1, 8 bool V_2) 9 il_0000: nop10 il_0001: LDC. i4.1 // push the integer 1 to the computing stack as int32. 12 il_0002: stloc.0 // The current value is displayed at the top of the computing stack and stored in the local variable list at index 0. 13 il_0003: LDC. i4.2 // push the integer 2 to the computing stack as int32. 15 il_0004: stloc.1 // same as above, the local variable list stored at Index 1 is 16 il_0005: ldloc.0 // load the local variable at index 0 to the computing stack. 17 il_0006: ldloc.1 // same as 18 il_0007: CEQ // if the two values are equal, the integer 1 (int32) is pushed to the computing stack; otherwise, 0 (int32) is pushed) push to the computing stack. 19 il_0009: stloc.2 // same as 20 il_000a: ret21} // end of method program: Main

Some comments are added to the Code, which generally means that the ushort is invisible to the int type, and the primitive type is determined by the CEQ command, similar to the following code:

1 static void Main()2  { 3       int i1 = 1;4       int i2 = 2;5       var b = i1 == i2;6  }

Instance. Equals will be explained in the next 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.