C # Boxing and unboxing Knowledge Review _c# tutorial

Source: Internet
Author: User

Boxing is a procedure that converts a value type to an object type or any interface type implemented by this value type. When the CLR boxing a value type, it wraps the value inside the System.Object and stores the latter on the managed heap. The unboxing extracts the value type from the object. The boxing is implicit, and the unboxing is explicit. The concept of boxing and unboxing is the basis for a unified view of type System C #, where any type of value is treated as an object.

In the following example, the integer variable i is boxed and assigned to object obj.

static void Main (string[] args)
  {
   var i = 123;//system.int32
   //To I boxing (implicit)
   object obj = i;
   Console.read ();
  }

Object obj can then be disassembled and assigned to the integer variable i.

static void Main (string[] args)
  {
   var i = 123;//system.int32
   //To I boxing (implicit)
   object obj = i;
   Unboxing (Explicit)
   i = (int) obj for obj;
   Console.read ();
  }

This demonstrates the boxed unboxing operation in code:

static void Main (string[] args)
  {
   //using String. The Format demonstrates the use of boxing, where 24 will be boxed to operate
   var formatstr = string. Format ("{0} {1}.", "I ' M",);
   Console.WriteLine ($ "formatstr: {formatstr}");
   var objs = new list<object> ();
   for (int i = 0; i < 5 i++)
   {
    //each time I will be boxed into the Objs
    Objs. ADD (i);
   }
   Console.WriteLine ("==========");
   foreach (Var obj in Objs)
   {
    //Two object types cannot be directly used *, you need to use int for explicit unboxing
    Console.WriteLine ($ "{obj} * * {obj} = {(i NT) obj * (int) obj} ");
   }
   Console.read ();
  }

Performance

The boxing and unboxing process requires a lot of calculation relative to the simple assignment. When boxing a value type, you must assign and construct a new object. The cast required for unpacking also requires a lot of calculation, but it is slightly lighter. If your operation is at the center of the loop, you will obviously feel the performance problem.

Packing 

Boxing is used to store value types in the heap. Boxing is an implicit conversion of a value type to an object type or to any interface type implemented by this value type. Boxing on a value type assigns an object instance to the heap and copies the value to the new object.

Consider the declaration of the following value type variable:

var i = 123;    //System.Int32

The following statement implicitly applies a boxing operation to the variable i:

 To I boxing (implicitly) into objects obj object
 obj = i;

The result of this statement is that the object reference O is created on the stack, and the value of type int is referenced on the heap. The value is a copy of the value type value assigned to the variable i. The following figure illustrates the difference between two variables I and O.

Of course, you can also choose to perform an explicit boxing, but explicit boxing is never required.

Split Box

A 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. The operation of the unboxing includes:

Examine the object instance to ensure that it is a boxed value of the given value type.

Copies the value from an instance to a value type variable.

   int i = 123;  Value type
   object o = i;  Boxed
   Int J = (int) o;//unboxing

To successfully split a value type at run time, the unpacking item must be a reference to an object that was previously created by boxing an instance of that value type. Attempting to cancel boxing null can cause NullReferenceException. Attempting to unboxing a reference to an incompatible value type can cause InvalidCastException.

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

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.