Principle of packing and unpacking

Source: Internet
Author: User

Knowledge points
    1. The value type.
      1. The value type allocates memory on the stack, is initialized at declaration time, and cannot be null.
      2. Value type out of scope system automatically frees memory.
      3. Mainly consists of two categories: structure, Enumeration (enum), structure is divided into the following categories:
        1. Integral type (Sbyte, Byte, Char, short, Ushort, Int, Uint, Long, Ulong)
        2. Floating-point type (float, Double)
        3. Decimal
        4. bool
        5. User-defined structure (struct)
    2. The reference type.
      1. The reference type allocates memory in the heap and defaults to NULL when initializing.
      2. Reference types are recycled through the garbage collection mechanism.
      3. Includes classes, interfaces, delegates, arrays, and built-in reference types, object and string.
Concept

Because all data types in C # are inherited by the base class System.Object, values of value types and reference types can be converted to and from each other through explicit (or implicit) operations, which are the boxing (boxing) and unboxing (unboxing) processes.

    1. Boxing is an implicit conversion of a value type to an object type or to any interface type implemented by this value type. Boxing a value type allocates an object instance in the heap and copies the value to the new object.
    2. unpacking (unboxing) is an explicit conversion from an object type to a value type or from an interface type to a value type that implements the interface. Unboxing operations include:
      1. Check the object instance to make sure it is a boxed value of the given value type. (There is no change to the original type after unpacking, there is no error at compile time, but the operation will be error, so make sure that.) Use GetType (). ToString () Make sure to use the full name of the type, such as: System.String instead of String. )

      2. Copy the value from the instance to the value type variable.

Example

Start by writing a simple console program:

Tutorial_boxing_unboxing.cs
Box packing and unpacking
Using System;
    1. Class APP
    2. {
    3. static void Main ()
    4. {
    5. int i = 32;
    6. Object o = i; Implicit boxing
    7. Console.WriteLine ("O = {0}", O);
    8. Console.read ();
    9. }
    10. }

Where object o = I here we have a boxing operation, and then we use the MSIL Disassembler to see the internal mechanism of the generated. EXE program.

Principle of packing and unpacking

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.