Difference between the value type and the reference type

Source: Internet
Author: User

Difference between the value type and the reference type
1. A Value Type object has two representation forms: unboxed and boxed. On the contrary, the reference type is always in the boxed form. 2. The value type is derived from System. ValueType. This type provides the same method as System. Object. However, System. ValueType overrides the Equals method and returns true if the field values of the two objects are completely matched. In addition, System. ValueType overrides the GetHashCode method. When a hash code is generated, the algorithm used by this rewrite method takes the value in the instance field of the object into consideration. Because this default implementation has performance problems, you should override the Equals and GetHashCode methods when defining your own value types and provide their display implementation 3, because the value type cannot be used as the base type to define a new value type or a new reference type, no new virtual method should be introduced in the value type, all methods cannot be abstract. All methods are implicitly sealed (not rewritable). 4. variables of the reference type include the address of the object in the heap. When a variable of the reference type is created, the initialization is null, and the table name does not point to a valid object currently. If the view uses a null reference type variable, an NUllReferenceException is thrown. On the contrary, variables of the Value Type always contain values of the basic type, and all members of the value type are initialized to 0. the value type variable is not a pointer, and the Access value type cannot throw NullReferenceExcption exception 5. Assign the value type variable to another value type variable and assign the value by field. Assign a value to a variable of the reference type to another variable of the reference type and only copy the memory address 6. Two or more references can reference the same object in the heap, therefore, operations performed on one variable may affect another referenced object. On the contrary, value type variables are self-contained, and the operations performed on value type variables cannot affect another value type variable 7, because unboxed value types are not allocated on the stack, once the methods for defining an example of this type are not active, the storage allocated to them will be released instead of waiting for garbage collection.

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.