Chapter 4 C # type of. net Programming pioneer

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 types 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 are only detected during compilation rather than runtime. 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 have 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 word size of a machine, while long is set to 64 bits.

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 obtained 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. Assign a value to a character variable as follows:
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 it is impossible to treat a character variable as another Integer Data Type in C # -- 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 1.0x10 ^-28 to 7.9x10 ^ 28 and has 28 to 29 valid digits. Note that the precision is expressed by digits rather than decimal places. The operation is accurate 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-conversion in one direction may overflow, and precision may be lost in another 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, 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 ++, a class can be defined with a structure keyword.
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 additional reference as required by the class object. 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 and so on, because these jobs are described in detail in the next chapter just like using 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 to declare a unique type composed of a specified constant set, the enumerated type is exactly what you are looking. The simplest form may look like this:
Enum MonthNames {January, February, March, limit L };
Because of the default settings, the enumeration element is of the int type, and the first element is 0. Each continuous element increments by 1. If you want to assign a value directly to the first element, you can set it to 1 as follows:
Enum MonthNames {January = 1, February, March, limit L };
If you want to assign any value to each element -- or even the same value -- this is no problem:
Enum MonthNames {January = 31, February = 28, March = 31, limit L = 30 };
The final selection is different from the int data type. You can assign values in a statement as follows:
Enum MonthNames: byte {January = 31, February = 28, March = 31, limit L = 30 };
You can use only long, int, short, and byte types.


4.2 reference type
Compared with the value type, the reference type does not store the actual data they represent, but stores the reference of the actual data. The following reference types are provided in C:
· Object type
· Category
· Interface
· Representative yuan
· String type
· Array

4.2.1 Object Type
Object type is the mother of all types-it is the most fundamental base class of other types. Because it is the base class of all objects, you can assign any type of value to it. For example, an integer:
Object theObj = 123;
Warn all C ++ programmers that the object is not equivalent to the void * you may be looking *. In any case, it is always a good idea to forget the pointer.
When a value type is framed (used as an object), the object type is used. This chapter will discuss the adding and removing boxes later.

4.2.2 category
A class type can contain data members, function members, and nested types. Data members are constants, fields, and events. Function members include methods, attributes, indexes, operators, constructors, and destructor. The functions of the class and structure are very similar, but as described above, the structure is a value type and the class is a reference type.
Compared with C ++, only single inheritance is allowed. (You cannot have a multi-duplicate base class that derives a new object .) However, a class in C # can be derived from multiple interfaces, which will be described in the next section.
Chapter 5 "Classes" specifically discusses the use of class programming. This section only provides a full picture of where the C # class fits the type chart.

4.2.3 Interface
An interface declares a reference type with only abstract members. Similar to the concept in C ++: a member of a structure, and the method is equal to 0. If you don't know anything about those concepts, here is what an interface in C # actually does. Only the method flag exists, but the code is not executed at all. This implies that one interface cannot be instantiated and only one object derived from this interface can be instantiated.
You can define methods, attributes, and indexes in an interface. So what are the special characteristics of interfaces compared to a class? When defining a class, you can derive from multiple interfaces, but you can only derive from only one class.
You may ask, "OK, but I have to implement all interface members. So what can I get from this channel? "I would like to give an example from. NET: many classes implement the IDictionary interface. You can use the simple type conversion access interface:
IDictionary myDict = (IDictionary) someobjectthatsupportsit;
Now your code can access the dictionary. So, you can reuse code in multiple places to access the IDictionary interface! Once learned, it can be used anywhere.
When you decide to use interfaces in class design

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.