[CLR via C #] 5.4 object hash code and dynamic primitive type

Source: Internet
Author: User

FCL designers believe that placing any instance of any object in a hash table set brings many benefits. Therefore, System. Object provides the virtual method GetHashCode, which can obtain the Int32 hash value of any Object.

If you override the Equals method, you should also override the GetHashCode method. Because in System. collection. hashtable type, System. collections. generic. in the Dictionary type and other set implementations, two objects must have the same hash code to be equal. Therefore, if Equals is rewritten, GetHashCode should also be rewritten, ensure that the equality algorithm is consistent with the object hash code algorithm. The GetHashCode implemented by System. ValueType adopts a reflection mechanism (which is slow) and performs XOR operations on instance fields of the type. We recommend that you implement GetHashCode by yourself so that you can know exactly what it is doing, and your implementation will be faster than the implementation of ValueType. When you implement a hash table set or call GetHashCode, do not persist the hash code, because the hash code is easy to change.

To make it easier for developers to use or communicate with basic components, the C # compiler allows the expression type to be marked as dynamic. you can also put the result of an expression in a variable and mark the type of the variable as dynamic. Then, you can use this dynamic expression/variable to call a member, for example, fields, attributes/indexers, methods, and delegation.

When the Code uses a dynamic expression or variable to call a member, the compiler generates special IL code to describe the required operations. This special code is called payload (payload ). At runtime, the payload Code determines the specific operation based on the actual type of the object currently referenced by dynamic expressions/variables.

Do not confuse dynamic and var. The local variables declared with var are just a simplified syntax that requires the compiler to deduce the specific data type based on an expression. The var keyword can only be used to declare local variables in a method, while the dynamic keyword can be used to declare local variables, fields, and parameters. The expression cannot be transformed into var, but dynamic. You must initialize the variables declared by var, but do not need to initialize the variables declared by dynamic.

The LS-DYNA expression is of the same type as System. Object. The compiler assumes that any operation on the expression is legal, so no warning or error is generated. However, if you try to execute an invalid operation at runtime, an exception is thrown.

You cannot define extension methods for dynamic extension, but you can define extension methods for Object extension.

Lambda expressions or anonymous methods cannot be used as arguments to call dynamic methods, because the compiler cannot deduce the type to be used.

C # The additional overhead produced by the built-in dynamic evaluation function cannot be ignored. Although the syntax can be simplified with dynamic functions, it also depends on whether it is worthwhile.

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.