Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the packing and unpacking of C #

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the packing and unpacking of C#". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is packing and unpacking of C#".

Boxing: converts a value type to a reference type.

Unboxing: converts a reference type to a value type.

Value types are relatively lightweight types that are not allocated in the managed heap like objects, are not GC, and are not referenced by pointers, but there are times when you need to obtain a reference to a value type, such as when using net1.0 's collection class ArrayList.

Class Program {static void Main (string [] args) {ArrayList list = new ArrayList (); Point pashing / because Point is a value type, assign for in the stack (int I = 0; I < 100; iSum +) {pp.x = p.y = I; / initialize member list.Add (p) in Point; / / after a pair of p is boxed, add references to list} struct Point {public Int32 x; public Int32 y } the Add method of ArrayList accepts an Object parameter, such as public virtual int Add (object value)

So when the Add method is executed, the Point value type is converted to a heap managed object, a reference to that object is obtained, and the reference address is stored in the ArrayList.

What happens inside a value type when it is boxed:

Allocate memory on the managed heap. The memory allocated is the amount of memory required for each field of the value type plus the two additional members on the managed heap (the type object pointer and the synchronous index block).

The field values in the l value type are copied to the newly allocated heap memory.

L returns the reference address of the object.

Unboxing is the opposite of boxing, converting a reference type to a value type. Follow the above code to get the element value in ArrayList with the following code:

For (int j = 0; j < 10; jacks +) {Point point = (Point) list [j]; Console.WriteLine ("X:" + point.x + "Y:" + point.y);}

In the above code, the reference addresses of each Point stored in ArrayList are taken by index, and the corresponding values are copied from the heap to the instance point of Point through Point type conversion. The conversion process is the process of unpacking.

Pay attention to the following two points in the process of unpacking:

1. If the variable referenced to a boxed value type is null, a NullRefreenceException exception is thrown

two。 If a reference points to an object that is not the type used when unboxing, an InvalidCastException exception will be thrown. The code is as follows:

Static void Main (string [] args) {Int32 x = 5; Object o = x; Int16 y = (Int16) obank / throw InvalidCastException exception}

The right thing to do is to unpack it with the Int32 type, and then cast it to Int16

Static void Main (string [] args) {Int32 x = 5; Object o = x; Int16 y = (Int16) (Int32) o;}

Let's take a look at two programs to gain an in-depth understanding of packing and unpacking.

Code 1:

Static void Main (string [] args) {Int32 x = 5; Object o = x; x = 123; Console.WriteLine (x + "," + (Int32) o);} Thank you for your reading, the above is the content of "what is the packing and unpacking of C#". After the study of this article, I believe you have a deeper understanding of what the packing and unpacking of C# is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report