Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 3 TYPES, STORAGE, AND VARIABLES

Predefined Types

C# provides 16 predefined types, which are shown in Figure 3-4 and listed in Tables 3-1 and 3-2. They include 13 simple types and 3 nonsimple types.

The names of all the predefined types consist of all lowercase characters. The predefined simple types include the following:

Eleven numeric types, including the following:

Various lengths of signed and unsigned integer types.

Floating-point types—float and double.

A high-precision decimal type called decimal. Unlike float and double, type decimal can represent decimal fractional numbers exactly. It’s often used for monetary calculations.

A Unicode character type, called char.

A Boolean type, called bool. Type bool represents Boolean values and must be one of two values—either true or false.

Note Unlike C and C++, numeric values do not have a Boolean interpretation in C#.

The three nonsimple types are the following:

Type string, which is an array of Unicode characters

Type object, which is the type on which all other types are based

Type dynamic, which is used when using assemblies written in dynamic languages

35

CHAPTER 3 TYPES, STORAGE, AND VARIABLES

Figure 3-4. The predefined types

More About the Predefined Types

All the predefined types are mapped directly to underlying .NET types. The C# type names are just aliases for the .NET types, so using the .NET names works fine syntactically, although this is discouraged. Within a C# program, you should use the C# names rather than the .NET names.

The predefined simple types represent a single item of data. They’re listed in Table 3-1, along with the ranges of values they can represent and the underlying .NET types to which they map.

36

CHAPTER 3 TYPES, STORAGE, AND VARIABLES

Table 3-1. The Predefined Simple Types

Name

Meaning

Range

.NET Framework

Default

 

 

 

Type

Value

 

 

 

 

 

sbyte

8-bit signed integer

-128–127

System.SByte

0

byte

8-bit unsigned integer

0–255

System.Byte

0

short

16-bit signed integer

-32,768–32,767

System.Int16

0

ushort

16-bit unsigned integer

0–65,535

System.UInt16

0

int

32-bit signed integer

-2,147,483,648–2,147,483,647

System.Int32

0

uint

32-bit unsigned integer

0–4,294,967,295

System.UInt32

0

long

64-bit signed integer

-9,223,372,036,854,775,808–

System.Int64

0

 

 

9,223,372,036,854,775,807

 

 

ulong

64-bit unsigned integer

0–18,446,744,073,709,551,615

System.UInt64

0

float

Single-precision float

1.5×10-45–3.4×1038

System.Single

0.0f

double

Double-precision float

5×10-324–1.7×10308

System.Double

0.0d

bool

Boolean

true, false

System.Boolean

false

char

Unicode character

U+0000–U+ffff

System.Char

\x0000

decimal

Decimal value with 28-

± 1.0×1028–±7.9×1028

System.Decimal

0m

 

significant-digit precision

 

 

 

 

 

 

 

 

The nonsimple predefined types are somewhat more complex. Values of type string contain zero or more Unicode characters. The object type is the base class for all other types in the system, including the predefined, simple types. Table 3-2 shows the predefined nonsimple types.

Table 3-2. The Predefined Nonsimple Types

Name

Meaning

.NET Framework Type

object

The base class from which all other types are derived

System.Object

string

A sequence of Unicode characters

System.String

dynamic

A type designed to be used with assemblies written in

 

 

dynamic languages

 

 

 

 

37

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]