C # conversion of value types and reference types in Study Notes (basic knowledge Review) (packing and unpacking ),

Source: Internet
Author: User

C # conversion of value types and reference types in Study Notes (basic knowledge Review) (packing and unpacking ),
1. For the meaning of the value type and reference type, refer to the previous article.

C # value type and reference type of study notes (basic knowledge Review)

1.1, C # data types are divided into the value type of memory allocation on the stack and the reference type of memory allocation on the managed stack. If int Is only a 4-byte value on the stack, how can we call the method on it?

Ii. Convert the value type to the reference type-packing

2.1CLR: When the value type is boxed, the managed heap memory is allocated, the instance fields of the value type are copied to the newly allocated memory, and the address of the newly allocated object in the managed heap is returned. This address is a reference to the object.

int i = 10;Object obj = i;

Iii. Convert the reference type to the value type -- binning

3.1 you can only unpack the deformation of the previous packing. The unpacking is to convert the object to the original type.

int i = 10;Object obj = i;int j = (int)obj;

4. Why does it need to be packed?

4.1 The most common scenario is to call a method containing parameters of the Object type. This Object can support any type for general purpose. When you need to input a value type, You need to pack it. For example, AddOne receives an Object-type parameter. If it is int32, the value is added to 1. If it is string, the string "1" is added ".

Static void Main (string [] args) {int I = 10; string str = "10"; Console. writeLine (AddOne (I); // output 11 Console. writeLine (AddOne (str); // output 101 Console. readKey ();} public static string AddOne (Object o) {if (o. getType () = typeof (Int32) {return (int) o + 1 ). toString ();} else if (o. getType () = typeof (String) {return o + "1" ;}else {return "1 ";}}

4.2 another usage is to define an element type as an Object to ensure the universality of a non-generic container. Therefore, to add the value type data to the container, You need to pack the data. For example:

Var array = new ArrayList (); array. add (1); array. add ("2"); foreach (var value in array) {Console. writeLine ("value is {0}", value);} // The result output is: value is 1 // value is 2 Console. readKey ();
V. Impact of packing and unpacking on Performance

From the principle, we can see that the new referenced object is generated during packing, which may cause time loss, that is, the efficiency is reduced.
Therefore, try to avoid packing.

For example, 4.1 of the cases can be avoided by means of heavy load, and 4.2 try to avoid box-breaking operations using generics.

 

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.