Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 11 Types

111. Types

2The types of the C# language are divided into three main categories: Value types, reference types , and type-

3parameter types.

4type:

5

6

7

value-type reference-type type-parameter

8Type parameters are part of generics, and are discussed in §26.1.1. A fourth category of types, pointers, is

9available only in unsafe code. This is discussed further in §25.2.

10Value types differ from reference types in that variables of the value types directly contain their data,

11whereas variables of the reference types store references to their data, the latter being known as objects.

12With reference types, it is possible for two variables to reference the same object, and thus possible for

13operations on one variable to affect the object referenced by the other variable. With value types, the

14variables each have their own copy of the data, and it is not possible for operations on one to affect the other.

15[Note: When a variable is a ref or out parameter, it does not have its own storage but references the

16storage of another variable. In this case, the ref or out variable is effectively an alias for another variable

17and not a distinct variable. end note]

18C#’s type system is unified such that a value of any type can be treated as an object. Every type in C#

19directly or indirectly derives from the object class type, and object is the ultimate base class of all types.

20Values of reference types are treated as objects simply by viewing the values as type object. Values of

21value types are treated as objects by performing boxing and unboxing operations (§11.3).

2211.1 Value types

23A value type is either a struct type or an enumeration type. C# provides a set of predefined struct types

24called the simple types. The simple types are identified through reserved words.

25value-type:

26

struct-type

27

enum-type

28

struct-type:

29

type-name

30

simple-type

31

simple-type:

32

numeric-type

33bool

34numeric-type:

35

integral-type

36

floating-point-type

37

decimal

105

 

C# LANGUAGE SPECIFICATION

1

integral-type:

2

sbyte

3

byte

4

short

5

ushort

6

int

7

uint

8

long

9

ulong

10char

11floating-point-type:

12 float

13double

14enum-type:

15

type-name

16All value types implicitly inherit from class object. It is not possible for any type to derive from a value

17type, and value types are thus implicitly sealed (§17.1.1.2).

18A variable of a value type always contains a value of that type. Unlike reference types, it is not possible for

19a value of a value type to be null, or to reference an object of a more derived type.

20Assignment to a variable of a value type creates a copy of the value being assigned. This differs from

21assignment to a variable of a reference type, which copies the reference but not the object identified by the

22reference.

2311.1.1 The System.ValueType type

24All value types implicitly inherit from the class System.ValueType, which, in turn, inherits from class

25object.

26Note that System.ValueType is not itself a value-type. Rather, it is a class-type from which all value-types

27are automatically derived.

2811.1.2 Default constructors

29All value types implicitly declare a public parameterless instance constructor called the default constructor.

30The default constructor returns a zero-initialized instance known as the default value for the value type:

31For all simple-types, the default value is the value produced by a bit pattern of all zeros:

32o For sbyte, byte, short, ushort, int, uint, long, and ulong, the default value is 0.

33o For char, the default value is '\x0000'.

34o For float, the default value is 0.0f.

35o For double, the default value is 0.0d.

36o For decimal, the default value is 0m.

37o For bool, the default value is false.

38For an enum-type E, the default value is 0.

39For a struct-type, the default value is the value produced by setting all value type fields to their default

40value and all reference type fields to null.

41Like any other instance constructor, the default constructor of a value type is invoked using the new

42operator. [Note: For efficiency reasons, this requirement is not intended to actually have the implementation

43generate a constructor call. For value types, the default value expression (§14.5.13) produces the same result

106

Chapter 11 Types

1as using the default constructor. end note] [Example: In the code below, variables i, j and k are all

2initialized to zero.

3class A

4{

5

void F() {

6

int i = 0;

7

int j = new int();

8

int k = default(int);

9}

10}

11end example]

12Because every value type implicitly has a public parameterless instance constructor, it is not possible for a

13struct type to contain an explicit declaration of a parameterless constructor. A struct type is however

14permitted to declare parameterized instance constructors (§18.3.8).

1511.1.3 Struct types

16A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators,

17instance constructors, static constructors, and nested types. Struct types are described in §18.

1811.1.4 Simple types

19C# provides a set of predefined struct types called the simple types. The simple types are identified through

20reserved words, but these reserved words are simply aliases for predefined struct types in the System

21namespace, as described in the table below.

22

Reserved word

Aliased type

 

 

sbyte

System.SByte

 

 

byte

System.Byte

 

 

short

System.Int16

 

 

ushort

System.UInt16

 

 

int

System.Int32

 

 

uint

System.UInt32

 

 

long

System.Int64

 

 

ulong

System.UInt64

 

 

char

System.Char

 

 

float

System.Single

 

 

double

System.Double

 

 

bool

System.Boolean

 

 

decimal

System.Decimal

 

 

23

24Because a simple type aliases a struct type, every simple type has members. [Example: int has the members

25declared in System.Int32 and the members inherited from System.Object, and the following

26statements are permitted:

27

int i = int.MaxValue;

// System.Int32.MaxValue constant

28

string s = i.ToString();

// System.Int32.ToString() instance method

29

string t = 123.ToString();

// System.Int32.ToString() instance method

30end example] The simple types differ from other struct types in that they permit certain additional

31operations:

107

C# LANGUAGE SPECIFICATION

1Most simple types permit values to be created by writing literals (§9.4.4). [Example: 123 is a literal of

2type int and 'a' is a literal of type char. end example] C# makes no provision for literals of struct

3types in general.

4When the operands of an expression are all simple type constants, the compiler evaluates the expression

5at compile-time. Such an expression is known as a constant-expression (§14.15). Expressions involving

6operators defined by other struct types are not considered to be constant expressions.

7Through const declarations, it is possible to declare constants of the simple types (§17.3). It is not

8possible to have constants of other struct types, but a similar effect is provided by static readonly

9fields.

10Conversions involving simple types can participate in evaluation of conversion operators defined by

11other struct types, but a user-defined conversion operator can never participate in evaluation of another

12user-defined conversion operator (§13.4.2).

1311.1.5 Integral types

14C# supports nine integral types: sbyte, byte, short, ushort, int, uint, long, ulong, and char. The

15integral types have the following sizes and ranges of values:

16The sbyte type represents signed 8-bit integers with values from –128 to 127, inclusive.

17The byte type represents unsigned 8-bit integers with values from 0 to 255, inclusive.

18The short type represents signed 16-bit integers with values from –32768 to 32767, inclusive.

19The ushort type represents unsigned 16-bit integers with values from 0 to 65535, inclusive.

20The int type represents signed 32-bit integers with values from –2147483648 to 2147483647,

21inclusive.

22The uint type represents unsigned 32-bit integers with values from 0 to 4294967295, inclusive.

23The long type represents signed 64-bit integers with values from –9223372036854775808 to

249223372036854775807, inclusive.

25The ulong type represents unsigned 64-bit integers with values from 0 to 18446744073709551615,

26inclusive.

27The char type represents unsigned 16-bit integers with values from 0 to 65535, inclusive. The set of

28possible values for the char type corresponds to the Unicode character set. [Note: Although char has

29the same representation as ushort, not all operations permitted on one type are permitted on the other.

30end note]

31The integral-type unary and binary operators always operate with signed 32-bit precision, unsigned 32-bit

32precision, signed 64-bit precision, or unsigned 64-bit precision, as detailed in clause §14.

33The char type is classified as an integral type, but it differs from the other integral types in two ways:

34There are no implicit conversions from other types to the char type. In particular, even though the

35sbyte, byte, and ushort types have ranges of values that are fully representable using the char type,

36implicit conversions from sbyte, byte, or ushort to char do not exist.

37Constants of the char type shall be written as character-literals or as integer-literals in combination

38with a cast to type char. [Example: (char)10 is the same as '\x000A'. end example]

39The checked and unchecked operators and statements are used to control overflow checking for integral-

40type arithmetic operations and conversions (§14.5.12). In a checked context, an overflow produces a

41compile-time error or causes a System.OverflowException to be thrown. In an unchecked context,

42overflows are ignored and any high-order bits that do not fit in the destination type are discarded.

108

Соседние файлы в предмете Электротехника