Reduced boxing and unboxing of effective C #

Source: Internet
Author: User
Tags tostring

In order to facilitate the development of the article, first introduced the boxing (Boxing) and unpacking (unboxing) these two nouns. NET type is divided into two types, one is the value type and the other is the reference type. The essential difference between the two types is that the value type data is allocated on the stack, and the reference type data is allocated on the heap. So if you want to put a value type data on the heap, you need boxing operations, or, conversely, put a value type on the heap to remove the data, you need to do a unboxing operation.

For example, for the following simple boxing and unboxing operation statements.

int i = 123;
  object obj = i;//Boxing
  if( obj is int )
  int j = (int) obj;//Unboxing

In order to better illustrate the boxing and unboxing operations, I borrowed the MSDN explanations for "Boxing", as detailed below.

Understand the meaning of these two names, now talk about why to reduce boxing and unboxing operations.

There are two reasons for this, mainly on efficiency: one is the low operating efficiency of the heap, and the other is that for memory resources allocated on the heap, GC is required to recycle, which reduces program efficiency.

With these two factors in mind, you need to reduce the boxing and unboxing operations in your program.

How to reduce it, involves the more of these two operations, the format of the output operation, such as: String.Format, Console.WriteLine, such as statements.

For example:

Console.WriteLine ("Number List:{0}, {1}, {2}", 1,2,3);

For "1,2,3", the equivalent of the previous "123", need to be boxed and unboxing two operations. So how to avoid, in fact, as long as the WriteLine Pass reference type data, that is, according to the following way.

Console.WriteLine ("Number List:{0}, {1}, {2}", 1.ToString (), 2.ToString (), 3.ToString ());

Because the result of "1.ToString ()" is a string type, it belongs to a reference type and is therefore not involved in boxing and unboxing operations.

Second, there are a lot of boxing and unboxing operations in the collection, such as: ArrayList or Hashtable.

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.