Brief introduction of C # Boxing and unboxing

Source: Internet
Author: User
Tags aliases bool integer range

The C # language is still more common, and here we introduce C # boxing and unboxing, including aspects such as invoking the Testalias () function.

C # Boxing and unboxing or aliases

Many c#.net books have introduced the int-> Int32 is a boxing process, whereas the reverse is the process of unpacking the box. This is true of many other variable types, such as short <-> int16,long <-> Int64. For the average programmer, it is not necessary to understand this process, because these C # boxing and unboxing actions can be automatically completed, do not need to write code to intervene. But we need to remember the relationships between these types, so we use "aliases" to remember the relationships between them.

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

What other classes have "aliases" Besides these three types? Commonly used are the following:

BOOL-> System.Boolean (Boolean, whose value is true or false)

Char-> System.Char (character type, occupies two bytes, representing 1 Unicode characters)

byte-> system.byte (byte type, 1 bytes, 8-bit positive integer, range 0 ~ 255)

SByte-> System.SByte (signed byte, accounting for 1 bytes, representing 8-bit integers, range-128 ~ 127)

ushort-> system.uint16 (unsigned short integer, accounting for 2 bytes, representing 16-bit positive integers, range 0 ~ 65,535)

UINT-> system.uint32 (unsigned integer, accounting for 4 bytes, representing 32-bit positive integers, range 0 ~ 4,294,967,295)

ULONG-> System.UInt64 (unsigned long integer, accounting for 8 bytes, representing 64-bit positive integers, range 0 ~ approximately 10 20 times)

Short-> system.int16 (shorter integer, 2 bytes, 16-bit integer, range-32,768 ~ 32,767)

int-> System.Int32 (integer, 4 bytes, representing 32-bit integers, range-2,147,483,648 to 2,147,483,647)

Long-> System.Int64 (8 bytes, representing 64-bit integers, range approximately-(10 of 19) to 10 of 19)

Float-> system.single (single-precision floating-point type, up to 4 bytes)

Double-> system.double (double-precision floating-point type, up to 8 bytes)

We can do an experiment with the following code:

private void TestAlias() {
// this.textBox1 是一个文本框,类型为 System.Windows.Forms.TextBox
// 设计中已经将其 Multiline 属性设置为 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");
}

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.