Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Praise for C# 2.0: Practical Guide for Programmers 2005 phần 4 pdf
MIỄN PHÍ
Số trang
22
Kích thước
370.4 KB
Định dạng
PDF
Lượt xem
1367

Praise for C# 2.0: Practical Guide for Programmers 2005 phần 4 pdf

Nội dung xem thử

Mô tả chi tiết

■ 4.3 Literals 63

4.3 Literals

The C# language has six literal types: integer, real, boolean, character, string, and null.

Integer literals represent integral-valued numbers. For example:

123 (is an integer by default)

0123 (is an octal integer, using the prefix 0)

123U (is an unsigned integer, using the suffix U)

123L (is a long integer, using the suffix L)

123UL (is an unsigned long integer, using the suffix UL)

0xDecaf (is a hexadecimal integer, using the prefix 0x)

Real literals represent floating-point numbers. For example:

3.14 .1e12 (are double precision by default)

3.1E12 3E12 (are double precision by default)

3.14F (is a single precision real, using the suffix F)

3.14D (is a double precision real, using the suffix D)

3.14M (is a decimal real, using the suffix M)

Suffixes may be lowercase but are generally less readable, especially when making the Tip

distinction between the number 1 and the letter l. The two boolean literals in C# are

represented by the keywords:

true false

The character literals are the same as those in C but also include the Unicode characters

(\udddd):

\ (continuation) ‘\n’ ‘\t’ ‘\b’ ‘\r’ ‘\f’ ‘\\’ ‘\’’ ‘\"’

0ddd or \ddd

0xdd or \xdd

0xdddd or \udddd

Therefore, the following character literals are all equivalent:

‘\n’ 10 012 0xA \u000A \x000A

String literals represent a sequence of zero or more characters—for example:

"A string"

"" (an empty string)

"\"" (a double quote)

Finally, the null literal is a C# keyword that represents a null reference.

64 Chapter 4: Unified Type System ■

4.4 Conversions

In developing C# applications, it may be necessary to convert or cast an expression of

one type into that of another. For example, in order to add a value of type float to a

value of type int, the integer value must first be converted to a floating-point number

before addition is performed. In C#, there are two kinds of conversion or casting: implicit

and explicit. Implicit conversions are ruled by the language and applied automatically

without user intervention. On the other hand, explicit conversions are specified by the

developer in order to support runtime operations or decisions that cannot be deduced by

the compiler. The following example illustrates these conversions:

1 // ‘a’ is a 16-bit unsigned integer.

2 int i = ‘a’; // Implicit conversion to 32-bit signed integer.

3 char c = (char)i; // Explicit conversion to 16-bit unsigned integer.

4

5 Console.WriteLine("i as int = {0}", i); // Output 97

6 Console.WriteLine("i as char = {0}", (char)i); // Output a

The compiler is allowed to perform an implicit conversion on line 2 because no information

is lost. This process is also called a widening conversion, in this case from 16-bit to 32-bit.

The compiler, however, is not allowed to perform a narrowing conversion from 32-bit to

16-bit on line 3. Attempting to do char c = i; will result in a compilation error, which

states that it cannot implicitly convert type int to type char. If the integer i must be

printed as a character, an explicit cast is needed (line 6). Otherwise, integer i is printed

as an integer (line 5). In this case, we are not losing data but printing it as a character,

a user decision that cannot be second-guessed by the compiler. The full list of implicit

conversions supported by C# is given in Table 4.4.

From To Wider Type

byte decimal, double, float, long, int, short, ulong, uint, ushort

sbyte decimal, double, float, long, int, short

char decimal, double, float, long, int, ulong, uint, ushort

ushort decimal, double, float, long, int, ulong, uint

short decimal, double, float, long, int

uint decimal, double, float, long, ulong

int decimal, double, float, long

ulong decimal, double, float

long decimal, double, float

float double

Table 4.4: Implicit conversions supported by C#.

Tải ngay đi em, còn do dự, trời tối mất!