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

A brief description of packing and unpacking in C #

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "A brief description of packing and unpacking in C#". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the "simple description of packing and unpacking in C#"!

C # packing and unpacking is still an alias

Many C#.NET books introduce that int-> Int32 is a process of packing, otherwise it is a process of unpacking. The same is true of many other variable types, such as short Int16,long Int64 and so on. For the average programmer, there is no need to understand this process, because these C# packing and unpacking actions can be done automatically without writing code to intervene. But we need to remember the relationship between these types, so we use aliases to remember the relationship between them.

C # is a fully object-oriented language that is more thorough than Java's object-oriented-it encapsulates simple data types into classes through default boxing actions. Int32, Int16, Int64, etc., are the corresponding class names, while the names that we are familiar with and easy to remember, such as int, short, long, etc., can be called aliases of Int32, Int16, Int64, and so on.

So apart from these three types, which other classes have aliases? The commonly used ones are as follows:

◆ bool-> System.Boolean (Boolean with values of true or false)

◆ char-> System.Char (character type, occupying two bytes, representing 1 Unicode character)

◆ byte-> System.Byte (byte type, 1 byte, indicating 8-bit positive integer, range 0-255)

◆ sbyte-> System.SByte (signed byte, 1 byte, indicating 8-bit integer, range-128127)

◆ ushort-> System.UInt16 (unsigned short integer, 2 bytes, 16-bit positive integer, range 0-65535)

◆ uint-> System.UInt32 (unsigned integer, 4 bytes, 32-bit positive integer, range 0-4294967295)

◆ ulong-> System.UInt64 (unsigned long integer, accounting for 8 bytes, representing 64-bit positive integers ranging from 0 to the 20th power of about 10)

◆ short-> System.Int16 (short integer, 2 bytes, 16-bit integer, range-32768-32767)

◆ int-> System.Int32 (integer, 4 bytes, 32-bit integer, range-2147483648 to 2147483647)

◆ long-> System.Int64 (long integers, 8 bytes, representing 64-bit integers, ranging from-(19) of 10 to 19 of 10)

◆ float-> System.Single (single precision floating point type, 4 bytes)

◆ double-> System.Double (double precision floating point type, 8 bytes)

We can do an experiment with the following code:

Private void TestAlias () {/ / this.textBox1 is a text box whose Multiline property has been set to true byte a = 1; char b = 'a'; short c = 1; int d = 2; long e = 3; uint f = 4; bool g = true; this.textBox1.Text = "; this.textBox1.AppendText (" byte-> "+ a.GetType (). FullName +"\ n ") This.textBox1.AppendText ("char->" + b.GetType (). FullName + "\ n"); this.textBox1.AppendText ("short->" + c.GetType (). FullName + "\ n"); this.textBox1.AppendText ("int->" + d.GetType (). FullName + "\ n"); this.textBox1.AppendText ("long->" + e.GetType (). FullName + "\ n") This.textBox1.AppendText ("uint->" + f.GetType (). FullName + "\ n"); this.textBox1.AppendText ("bool->" + g.GetType (). FullName + "\ n");}

Create a new button in the form and call the TestAlias () function in its click event, and we will see the running result as follows:

Byte-> System.Byte char-> System.Char short-> System.Int16 int-> System.Int32 long-> System.Int64 uint-> System.UInt32 bool-> System.Boolean so far, I believe you have a deeper understanding of the "simple description of packing and unpacking in C#". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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