Two representations of value type objects

Source: Internet
Author: User
1, the value type object has two representations: unboxed and boxed, whereas the reference type is always in boxed form 2 and the value type is derived from System.ValueType. This type provides the same method as System.Object. However, System.ValueType overrides the Equals method to return True if the field values of the two objects match exactly. Additionally, System.ValueType overrides the GetHashCode method. When generating a hash code, the algorithm used by this override method takes into account the value in the instance field of the object. Because of the performance problem with this default implementation, you should override the Equals and GetHashCode methods when defining your own value types, and provide their display implementation 3, because you cannot define a new value type or a new reference type as a base type, so you should not introduce any new virtual methods in the value type , all methods cannot be abstract, all methods are implicitly sealed (non-Overridable) 4, and variables of reference types contain the addresses of objects in the heap. When a variable of a reference type is created, it is initialized to null, and the table name does not currently point to a valid object. The view uses a null reference type variable to throw a NullReferenceException exception. Instead, a variable of value type always contains the value of the underlying type, and all members of the value type are initialized to 0. value type variable is not a pointer, access value type cannot throw Nullreferenceexcption exception 5, assign value type variable to another value type variable, will execute literal paragraph assignment. Assigning a variable of a reference type to a variable of another reference type replicates only memory address 6, where two or more references can refer to the same object in the heap, so operations on one variable may affect another referenced object. Conversely, a value type variable self-contained, an operation performed on a value type variable cannot affect another value type variable 7, because the unboxed value type is not allocated on the heap, and once the method that defines an example of that type is not active, the storage allocated to them is freed instead of waiting for garbage collection
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.