C # Reference types and value types

Source: Internet
Author: User

C # Reference types and value types

The CLR supports two types: reference types and value types.

1. Reference types

(1) The memory must be allocated from the managed heap;

(2) Each object allocated on the heap has some additional members (including "type Object pointer", "Synchronous Block Index"), which must be initialized;

(3) The other bytes of the object are always set to zero;

(4) When an object is allocated from the managed heap, a garbage collection may be enforced.

2. Value types

(1) General thread stack allocation;

(2) not controlled by the garbage collector;

3. The difference between the two

(1) Two representations of value types: unboxed and boxed, and reference types are boxed;

(2) The value type derives from System.ValueType, provides the same method as System.Object, but overrides the Equals and GetHashCode methods;

(3) Because value types cannot be base types, virtual methods should not be used in value types;

(4) The reference type contains the address of the object in the heap, and defaults to null;

(5) Assigning a value type variable to another value type variable duplicates the value of each field, while the reference type copies only the memory address, so the copied object that changes the value type does not affect the original value, but the reference type changes the original value;

(6) because an unpackaged value type is not allocated on the heap, its memory is freed if the case of that type is no longer active after the assignment.

4. Packing

Definition: Converts a value type to a reference type.

(1): Allocate the memory required for each field of the value type in heap memory plus additional members (type object pointer and synchronous block index);

(2): The value Type field is copied to the newly allocated internal deposit;

(3): Returns the object address.

5. Unpacking

Definition: Converts a reference type to a value type.

(1): Gets the field values of the boxed case;

(2): The value type that copies the field's values from the heap to the stack.

Attention:

(1): Throws a NullReferenceException exception if the reference to the boxed case is null;

(2): Throws a InvalidCastException exception if the referenced object is not a boxed instance of the desired value type.

For example:

1  Public Static void Main () 2 {3      5 ; 4      Object o = x;    5      Int64 y = (Int64) (Int32) O; 6 }

C # Reference types and value types

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.