Chapter 4 C # type

Source: Internet
Author: User

Chapter 4 C # type

Now that you know how to create a simple C # program, I will introduce you to the C # type system. In this chapter, you will learn how to use different values and reference types, and what the frame adding and box removing mechanisms can do for you. Although this chapter does not focus on examples, you can learn a lot about how to create a ready-to-use program.
4.1 Value Type
Each value type always contains a value of the corresponding type. C # forces you to initialize variables before using them for computing-variables without initialization will not cause problems, because the compiler will tell you when you attempt to use them.
Each time a value is assigned to a value type, the value is actually copied. In contrast, for the reference type, only the reference is copied, and the actual value is still kept in the same memory location, but now two objects point to it (reference it ). C # value class
Type can be classified as follows:
· Simple types)
· Structure Type (struct types)
· Enumeration types)
4.1.1 simple type
Some features of simple type sharing in C. First, they are all aliases of the. NET system type. Second, constant expressions composed of simple types only
This vulnerability is not detected during compilation. Finally, simple types can be initialized literally. C # simple type classification:
· Integer
· Boolean
· Balanced type (a special case of Integer type)
· Floating point type
· Decimal type

4.1.1.1 integer
C # contains 9 integers. Sbyte, byte, short, ushort, int, uint, long, ulong, and char (discussed in a separate section ). They
Has the following features:

· Sbyte is a signed 8-digit integer with a value range of 128 ~ In the range of 127.
· Bytet is an unsigned 16-bit integer with a value ranging from 0 ~ In the range of 255.
· The short type is a signed 16-digit integer with a value ranging from-32,768 ~ In the range of 32,767.
· Ushort is an unsigned 16-bit integer with a value ranging from 0 ~ In the range of 65,535.
· The int type is a signed 32-bit integer with a value range of-2,147,483,648 ~ In the range of 2,147,483,647.
· Uint is a 32-bit unsigned integer with a value range of 0 ~ In the range of 4,294,967,295.
· Long is a 64-bit signed integer with a value range of 9,223,372,036,854,775,808 ~ In the range of 9,223,372,036,854,775,807.
· Ulong is a 64-bit unsigned integer with a value ranging from 0 ~ In the range of 18,446,744,073,709,551,615.

Both VB and C programmers may be surprised by the new range represented by int and long data types. Compared with other programming languages, in C #, int no longer depends on
The size of a machine's word, while long is set to 64-bit.

4.1.1.2 Boolean
Boolean data types include true and false. You can assign true or false values to a Boolean variable or an expression.
The output value is equal to one of the two:
Bool bTest = (80> 90 );
Compared with C and C ++, in C #, true is no longer a non-zero value. Do not convert other integer types to a boolean type for convenience.

4.1.1.3 accept type
Character type is a single Unicode character. A Unicode character is 16-bit long and can be used to represent multiple languages in the world. You can give a word as follows:
Variable Value assignment:
Char chSomeChar =;
In addition, you can assign a value to the variable (prefix u) Using the hexadecimal escape character (prefix x) or Unicode Notation ):
Char chSomeChar = x0065;
Char chSomeChar = u0065;
There is no implicit conversion to convert char to other data types. This means that a character variable in C # is treated as another Integer Data Type
This is another aspect that C programmers must change their habits. However, Explicit conversions can be used:
Char chSomeChar = (char) 65;
Int nSomeInt = (int);
There are still escape characters (character meanings) in C ). To change your mind, see table 4.1.

Table 4.1 Escape character (Escape Sequences)

Escape Character name
Single quotes
"Double quotation marks
\ Backslash
NULL Character
A exclamation point (Alert)
Return
F form feed
New Line
Enter
Horizontal tab
V vertical tab

4.1.1.4 floating point type
The two data types are treated as float and double. Their difference lies in the value range and accuracy:
Float: The value range is 1.5x10 ^-45 ~ Between 3.4x10 ^ 38, the precision is 7 digits.
Double: The value range is 5.0x10 ^-324 ~ Between 1.7x10 ^ 308, precision is 15 ~ 16 digits.
When two floating point types are used to execute an operation, the following values can be generated:
Positive and Negative
Positive infinity and negative infinity
Non-numeric value (Not-a-Number, NaN)
A finite number of non-zero values
Another calculation rule is that when one value in the expression is a floating point type, all other types must be converted to the floating point type for calculation.

4.1.1.5 decimal Type)
Decimal is a high-precision, 128-bit data type, which is intended for financial and currency computing. It ranges from about 1.0x10 ^-28
7.9x10 ^ 28, with 28 to 29 valid digits. Note that the precision is expressed by digits rather than decimal places. Accurate operation
To the maximum of 28 decimal places.
As you can see, its value range is narrower than that of double, but it is more accurate. Therefore, there is no implicit conversion between decimal and double -- to 1
Direction conversion may overflow, and precision may be lost in the other direction. You have to use Explicit conversions.
When a variable is defined and assigned to it, the m suffix is used to indicate that it is a decimal type:
Decimal decMyValue = 1.0 m;
If m is omitted, the variable is considered double by the compiler before it is assigned a value.

4.1.2 Structure Type
A structure type can declare constructors, constants, fields, methods, attributes, indexes, operators, and nested types. Although the listed functions look like
A mature class, but in C #, the difference between the structure and the class is that the structure is a value type, and the class is a reference type. Compared with C ++, the structure
A key defines a class.
The main idea of using the structure is to create small objects, such as Point and FileInfo. You can save memory because there is no such thing
There are additional references. For example, when declaring an array containing thousands of objects, this can cause a huge difference.
Listing 4.1 contains a simple structure named IP, which represents an IP address that uses four fields of the byte type. I do not include methods, etc.
The work is described in the next chapter just like the use of classes.

Listing 4.1 defines a simple structure

1: using System;
2:
3: struct IP
4 :{
5: public byte b1, b2, b3, b4;
6 :}
7:
8: class Test
9 :{
10: public static void Main ()
11 :{
12: IP myIP;
13: myIP. b1 = 192;
14: myIP. b2= 168;
15: myIP. b3 = 1;
16: myIP. b4 = 101;
17: Console. Write ("{0}. {1}.", myIP. b1, myIP. b2 );
18: Console. Write ("{0}. {1}", myIP. b3, myIP. b4 );
19 :}
20 :}

4.1.3 Enumeration type
When you want

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.