值类型
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.