Home  >  Article  >  Backend Development  >  Two representations of value type objects

Two representations of value type objects

零下一度
零下一度Original
2017-06-23 15:40:281593browse
1. Value type objects have two representations: unboxed and boxed. On the contrary, reference types are always in the boxed form.
2. Value types come from System .ValueType is derived. This type provides the same methods as System.Object. However, System.ValueType overrides the Equals method and can return true if the field values ​​of the two objects completely match. In addition, System.ValueType overrides the GetHashCode method. The algorithm used by this overridden method takes into account the values ​​in the object's instance fields when generating the hash code. Since this default implementation has performance issues, you should override the Equals and GetHashCode methods when defining your own value type, and provide their explicit implementation
3, because the value type cannot be used as a base Type to define a new value type or a new reference type, so no new virtual methods should be introduced in value types, all methods cannot be abstract, and all methods are implicitly sealed (not overridable)
4. Reference type variables contain the address of the object in the heap. Reference type variables are initialized to null when created, and the table name currently does not point to a valid object. Views that use null reference type variables will throw NUllReferenceException. On the contrary, a variable of a value type always contains a value of the underlying type, and all members of the value type are initialized to 0. A value type variable is not a pointer, and accessing a value type cannot throw a NullReferenceExcption exception
5, assigning a value type variable to another value type variable will perform field-by-field assignment. Assigning a reference type variable to another reference type variable only copies the memory address
6. Two or more references can refer to the same object in the heap, so for a variable The operation performed may affect another referenced object. In contrast, a value type variable is self-contained, and operations performed on a value type variable cannot affect another value type variable
7. Since unboxed value types are not allocated on the heap, Once the methods that define an instance of the type are no longer active, the storage allocated for them is released instead of waiting for garbage collection

The above is the detailed content of Two representations of value type objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn