Value Type, reference type, packing, unpacking

Source: Internet
Author: User

  • The value type includes the original type (Sbyte, byte, short, ushort, Int, uint, long, ulong, Char, float, double, bool, decimal), Enumeration (Enum), Structure (Struct), Reference types include: Class, array, interface, Delegate, String, etc.
  • The value type is used to allocate memory in the stack and initialize it at the same time as the Declaration to ensure that the data is not null;
    The reference type allocates memory in the heap and is initialized to null. The reference type requires garbage collection to recycle the memory. If the value type is not used, the system will automatically release the memory when it exceeds the scope of the function!
  • Variables of the value type contain their own data, while variables of the reference type point to the memory block of the data, and do not directly store the data. For value types, each variable has its own data copy. Operations on another value type variable do not affect the value of this variable. For the reference type, two variables may reference the same object. Therefore, operations on one variable may affect the other.

  • If you want to put a value type data on the stack, You need to pack the data. Otherwise, you need to unpack the value type data on the stack.

  • Packing is to convert the value typeObjectType or any interface type implemented by this value type.When the CLR loads the value type, the value is encapsulated into system. object, and then stored on the managed stack. Unboxing extracts the value type from the object. Packing is implicit; unboxing is explicit. The concept of packing and unboxing is the basis of the unified view of type system C #. Any type of value is regarded as an object.
  • Packing is used to store value types in the garbage collection heap.Packing is a value typeObjectType or any interface type implemented by this value type. Binning the value type will allocate an object instance in the heap and copy the value to the new object.
  • unboxing is performed from Object type to value type, or an explicit conversion from the interface type to the value type of the interface. unboxing:

    • check the object instance to make sure it is a boxed value of the given value type.

    • copy the value from the instance to the value type variable.

  • To successfully cancel the value type during running, the unboxing item must be a reference to an object, which was previously created by packing the value type instance.Try to unboxingNullWill causeNullreferenceexception. An invalidcastexception occurs when you try to cancel the reference of incompatible value types.
  • Packing and unpacking process:
  • Why do we need to reduce the number of packing and unpacking operations:One is the low efficiency of heap operations, and the other is the memory resources allocated on the heap.GCTo reduce program efficiency.

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.