4. Types

本文详细介绍了C#中的值类型和引用类型,包括简单的值类型如整数和浮点数,以及枚举类型和可空类型的概念。同时,还探讨了类类型、接口类型、数组类型和委托类型的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

值类型

A value type is either a struct type or anenumeration type. C# provides a set of predefined struct types called the simple types. The simple types are identified throughreserved words.

.1 The System.ValueType typeSystem.ValueTypeSystem.ValueTypeSystem.ValueType

System.ValueType

All value types implicitly inherit from theclass System.ValueType, which, in turn, inherits from class object. It is not possible for any type to derive from a value type, andvalue types are thus implicitly sealed (§10.1.1.2).

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


A struct type is a value type that candeclare constants, fields, methods, properties, indexers, operators, instanceconstructors, static constructors, and nested types. The declaration of struct typesis described in §11.1.


C# provides a set of predefined structtypes called the simple types. The simple types areidentified through reserved words, but these reserved words are simply aliasesfor predefined struct types in the Systemnamespace, as described in the table below.

 

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

 

Because a simple type aliases a structtype, every simple type has members. For example, int has the members declared in System.Int32and the members inherited from System.Object


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


C# supports two floating point types: float and double.


The decimal type is a 128-bitdata type suitable for financial and monetary calculations.


The bool type representsboolean logical quantities. 

An enumeration type is a distinct type withnamed constants. Every enumeration type has an underlying type, which must be byte, sbyte, short, ushort, int, uint, long or ulong. The set of valuesof the enumeration type is the same as the set of values of the underlyingtype. Values of the enumeration type are not restricted to the values of thenamed constants. Enumeration types are defined through enumeration declarations 


A nullable type can represent all values ofits underlying type plus an additional null value. Anullable type is written T?, where T is the underlying type. This syntaxis shorthand for System.Nullable<T>, and the two forms can be used interchangeably.

A non-nullable value typeconversely is any value type other than System.Nullable<T> and its shorthand T? (for any T), plus any type parameter that is constrained to be a non-nullablevalue type (that is, any type parameter with a structconstraint). The System.Nullable<T> type specifies the value type constraint for T (§10.1.5),which means that the underlying type of a nullable type can be anynon-nullable value type. The underlying type of a nullable type cannot be anullable type or a reference type. For example, int?? and string? are invalidtypes.

An instance of a nullable type T? has two public read-only properties:

·        A HasValue property of type bool

·        A Value property of type T


引用类型  reference type

Class type

Description

System.Object

The ultimate base class of all other types. See §4.2.2.

System.String

The string type of the C# language. See §4.2.4.

System.ValueType

The base class of all value types. See §4.1.1.

System.Enum

The base class of all enum types. See §14.

System.Array

The base class of all array types. See §12.

System.Delegate

The base class of all delegate types. See §15.

System.Exception

The base class of all exception types. See §16.



A reference type is a class type, aninterface type, an array type, or a delegate type.

 

1.1.1 The object type

The object classtype is the ultimate base class of all other types. Every type in C# directlyor indirectly derives from the object classtype.

The keyword object is simply an alias for the predefined class System.Object.

1.1.2 Thedynamic type

The dynamictype, like object, can reference any object. Whenoperators are applied to expressions of type dynamic,their resolution is deferred until the program is run. Thus, if the operatorcannot legally be applied to the referenced object, no error is given duringcompilation. Instead an exception will be thrown when resolution of the operatorfails at run-time.

The dynamic type is further described in §4.7, anddynamic binding in §7.2.2.

1.1.3 The string type

The string typeis a sealed class type that inherits directly from object. Instances of the string class representUnicode character strings.

Values of the string type can be written as string literals (§2.4.4.5).

The keyword string is simply an alias for the predefined class System.String.

1.1.4 Interfacetypes

An interface defines a contract. A class orstruct that implements an interface must adhere to its contract. An interfacemay inherit from multiple base interfaces, and a class or struct may implementmultiple interfaces.

Interface types are described in §13.

1.1.5 Arraytypes

An array is a data structure that containszero or more variables which are accessed through computed indices. Thevariables contained in an array, also called the elements of the array, are allof the same type, and this type is called the element type of the array.

Array types are described in §12.

1.1.6 Delegatetypes

A delegate is a data structure that refersto one or more methods. For instance methods, it also refers to their correspondingobject instances.

The closest equivalent of a delegate in Cor C++ is a function pointer, but whereas a function pointer can only referencestatic functions, a delegate can reference both static and instance methods. Inthe latter case, the delegate stores not only a reference to the method’s entrypoint, but also a reference to the object instance on which to invoke themethod.

Delegate types are described in §15.
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值