C# Types
date
Nov 8, 2021
slug
10014
status
Published
tags
C#
summary
type
Post
The types of the C# language are divided into two main categories: value types and reference types. Both value types and reference types may be generic types, which take one or more type parameters. Type parameters can designate both value types and reference types.
type
: value_type
| reference_type
| type_parameter
| type_unsafe
;The final category of types, pointers, is available only in unsafe code. This is discussed further in Pointer types.
Value types differ from reference types in that variables of the value types directly contain their data, whereas variables of the reference types store references to their data, the latter being known as objects. With reference types, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other.
C#'s type system is unified such that a value of any type can be treated as an object. Every type in C# directly or indirectly derives from the
object class type, and object is the ultimate base class of all types. Values of reference types are treated as objects simply by viewing the values as type object. Values of value types are treated as objects by performing boxing and unboxing operations (Boxing and unboxing).Value types
A value type is either a struct type or an enumeration type. C# provides a set of predefined struct types called the simple types. The simple types are identified through reserved words.
value_type
: struct_type
| enum_type
;
struct_type
: type_name
| simple_type
| nullable_type
;
simple_type
: numeric_type
| 'bool'
;
numeric_type
: integral_type
| floating_point_type
| 'decimal'
;
integral_type
: 'sbyte'
| 'byte'
| 'short'
| 'ushort'
| 'int'
| 'uint'
| 'long'
| 'ulong'
| 'char'
;
floating_point_type
: 'float'
| 'double'
;
nullable_type
: non_nullable_value_type '?'
;
non_nullable_value_type
: type
;
enum_type
: type_name
;Unlike a variable of a reference type, a variable of a value type can contain the value
null only if the value type is a nullable type. For every non-nullable value type there is a corresponding nullable value type denoting the same set of values plus the value null.Assignment to a variable of a value type creates a copy of the value being assigned. This differs from assignment to a variable of a reference type, which copies the reference but not the object identified by the reference.
The System.ValueType type
All value types implicitly inherit from the class
System.ValueType, which, in turn, inherits from class object. It is not possible for any type to derive from a value type, and value types are thus implicitly sealed (Sealed classes).Note that
System.ValueType is not itself a value_type. Rather, it is a class_type from which all value_types are automatically derived.Default constructors
All value types implicitly declare a public parameterless instance constructor called the default constructor. The default constructor returns a zero-initialized instance known as the default value for the value type:
- For all simple_types, the default value is the value produced by a bit pattern of all zeros:
- For
sbyte,byte,short,ushort,int,uint,long, andulong, the default value is0. - For
char, the default value is'\x0000'. - For
float, the default value is0.0f. - For
double, the default value is0.0d. - For
decimal, the default value is0.0m. - For
bool, the default value isfalse.
- For an enum_type
E, the default value is0, converted to the typeE.
- For a struct_type, the default value is the value produced by setting all value type fields to their default value and all reference type fields to
null.
- For a nullable_type the default value is an instance for which the
HasValueproperty is false and theValueproperty is undefined. The default value is also known as the null value of the nullable type.
Like any other instance constructor, the default constructor of a value type is invoked using the
new operator. For efficiency reasons, this requirement is not intended to actually have the implementation generate a constructor call. In the example below, variables i and j are both initialized to zero.class A
{
void F() {
int i = 0;
int j = new int();
}
}Because every value type implicitly has a public parameterless instance constructor, it is not possible for a struct type to contain an explicit declaration of a parameterless constructor. A struct type is however permitted to declare parameterized instance constructors (Constructors).
Struct types
A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types. The declaration of struct types is described in Struct declarations.
Simple types
C# provides a set of predefined struct types called the simple types. The simple types are identified through reserved words, but these reserved words are simply aliases for predefined struct types in the
System namespace, as described in the table below.Reserved word
Aliased type
sbyteSystem.SBytebyteSystem.ByteshortSystem.Int16ushortSystem.UInt16intSystem.Int32uintSystem.UInt32longSystem.Int64ulongSystem.UInt64charSystem.CharfloatSystem.SingledoubleSystem.DoubleboolSystem.BooleandecimalSystem.DecimalBecause a simple type aliases a struct type, every simple type has members. For example,
int has the members declared in System.Int32 and the members inherited from System.Object, and the following statements are permitted:int i = int.MaxValue; // System.Int32.MaxValue constant
string s = i.ToString(); // System.Int32.ToString() instance method
string t = 123.ToString(); // System.Int32.ToString() instance methodThe simple types differ from other struct types in that they permit certain additional operations:
- Most simple types permit values to be created by writing literals (Literals). For example,
123is a literal of typeintand'a'is a literal of typechar. C# makes no provision for literals of struct types in general, and non-default values of other struct types are ultimately always created through instance constructors of those struct types.
- When the operands of an expression are all simple type constants, it is possible for the compiler to evaluate the expression at compile-time. Such an expression is known as a constant_expression (Constant expressions). Expressions involving operators defined by other struct types are not considered to be constant expressions.
- Through
constdeclarations it is possible to declare constants of the simple types (Constants). It is not possible to have constants of other struct types, but a similar effect is provided bystatic readonlyfields.
- Conversions involving simple types can participate in evaluation of conversion operators defined by other struct types, but a user-defined conversion operator can never participate in evaluation of another user-defined operator (Evaluation of user-defined conversions).
Integral types
C# supports nine integral types:
sbyte, byte, short, ushort, int, uint, long, ulong, and char. The integral types have the following sizes and ranges of values:- The
sbytetype represents signed 8-bit integers with values between -128 and 127.
- The
bytetype represents unsigned 8-bit integers with values between 0 and 255.
- The
shorttype represents signed 16-bit integers with values between -32768 and 32767.
- The
ushorttype represents unsigned 16-bit integers with values between 0 and 65535.
- The
inttype represents signed 32-bit integers with values between -2147483648 and 2147483647.
- The
uinttype represents unsigned 32-bit integers with values between 0 and 4294967295.
- The
longtype represents signed 64-bit integers with values between -9223372036854775808 and 9223372036854775807.
- The
ulongtype represents unsigned 64-bit integers with values between 0 and 18446744073709551615.
- The
chartype represents unsigned 16-bit integers with values between 0 and 65535. The set of possible values for thechartype corresponds to the Unicode character set. Althoughcharhas the same representation asushort, not all operations permitted on one type are permitted on the other.
The integral-type unary and binary operators always operate with signed 32-bit precision, unsigned 32-bit precision, signed 64-bit precision, or unsigned 64-bit precision:
- For the unary
+and~operators, the operand is converted to typeT, whereTis the first ofint,uint,long, andulongthat can fully represent all possible values of the operand. The operation is then performed using the precision of typeT, and the type of the result isT.
- For the unary
operator, the operand is converted to typeT, whereTis the first ofintandlongthat can fully represent all possible values of the operand. The operation is then performed using the precision of typeT, and the type of the result isT. The unaryoperator cannot be applied to operands of typeulong.
- For the binary
+,,,/,%,&,^,|,==,!=,>,<,>=, and<=operators, the operands are converted to typeT, whereTis the first ofint,uint,long, andulongthat can fully represent all possible values of both operands. The operation is then performed using the precision of typeT, and the type of the result isT(orboolfor the relational operators). It is not permitted for one operand to be of typelongand the other to be of typeulongwith the binary operators.
- For the binary
<<and>>operators, the left operand is converted to typeT, whereTis the first ofint,uint,long, andulongthat can fully represent all possible values of the operand. The operation is then performed using the precision of typeT, and the type of the result isT.
The
char type is classified as an integral type, but it differs from the other integral types in two ways:- There are no implicit conversions from other types to the
chartype. In particular, even though thesbyte,byte, andushorttypes have ranges of values that are fully representable using thechartype, implicit conversions fromsbyte,byte, orushorttochardo not exist.
- Constants of the
chartype must be written as character_literals or as integer_literals in combination with a cast to typechar. For example,(char)10is the same as'\x000A'.
The
checked and unchecked operators and statements are used to control overflow checking for integral-type arithmetic operations and conversions (The checked and unchecked operators). In a checked context, an overflow produces a compile-time error or causes a System.OverflowException to be thrown. In an unchecked context, overflows are ignored and any high-order bits that do not fit in the destination type are discarded.Floating point types
C# supports two floating point types:
float and double. The float and double types are represented using the 32-bit single-precision and 64-bit double-precision IEEE 754 formats, which provide the following sets of values:- Positive zero and negative zero. In most situations, positive zero and negative zero behave identically as the simple value zero, but certain operations distinguish between the two (Division operator).
- Positive infinity and negative infinity. Infinities are produced by such operations as dividing a non-zero number by zero. For example,
1.0 / 0.0yields positive infinity, and1.0 / 0.0yields negative infinity.
- The Not-a-Number value, often abbreviated NaN. NaNs are produced by invalid floating-point operations, such as dividing zero by zero.
- The finite set of non-zero values of the form
s * m * 2^e, wheresis 1 or -1, andmandeare determined by the particular floating-point type: Forfloat,0 < m < 2^24and149 <= e <= 104, and fordouble,0 < m < 2^53and1075 <= e <= 970. Denormalized floating-point numbers are considered valid non-zero values.
The
float type can represent values ranging from approximately 1.5 * 10^-45 to 3.4 * 10^38 with a precision of 7 digits.The
double type can represent values ranging from approximately 5.0 * 10^-324 to 1.7 × 10^308 with a precision of 15-16 digits.If one of the operands of a binary operator is of a floating-point type, then the other operand must be of an integral type or a floating-point type, and the operation is evaluated as follows:
- If one of the operands is of an integral type, then that operand is converted to the floating-point type of the other operand.
- Then, if either of the operands is of type
double, the other operand is converted todouble, the operation is performed using at leastdoublerange and precision, and the type of the result isdouble(orboolfor the relational operators).
- Otherwise, the operation is performed using at least
floatrange and precision, and the type of the result isfloat(orboolfor the relational operators).
The floating-point operators, including the assignment operators, never produce exceptions. Instead, in exceptional situations, floating-point operations produce zero, infinity, or NaN, as described below:
- If the result of a floating-point operation is too small for the destination format, the result of the operation becomes positive zero or negative zero.
- If the result of a floating-point operation is too large for the destination format, the result of the operation becomes positive infinity or negative infinity.
- If a floating-point operation is invalid, the result of the operation becomes NaN.
- If one or both operands of a floating-point operation is NaN, the result of the operation becomes NaN.
Floating-point operations may be performed with higher precision than the result type of the operation. For example, some hardware architectures support an "extended" or "long double" floating-point type with greater range and precision than the
double type, and implicitly perform all floating-point operations using this higher precision type. Only at excessive cost in performance can such hardware architectures be made to perform floating-point operations with less precision, and rather than require an implementation to forfeit both performance and precision, C# allows a higher precision type to be used for all floating-point operations. Other than delivering more precise results, this rarely has any measurable effects. However, in expressions of the form x * y / z, where the multiplication produces a result that is outside the double range, but the subsequent division brings the temporary result back into the double range, the fact that the expression is evaluated in a higher range format may cause a finite result to be produced instead of an infinity.The decimal type
The
decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type can represent values ranging from 1.0 * 10^-28 to approximately 7.9 * 10^28 with 28-29 significant digits.The finite set of values of type
decimal are of the form (-1)^s * c * 10^-e, where the sign s is 0 or 1, the coefficient c is given by 0 <= *c* < 2^96, and the scale e is such that 0 <= e <= 28.The decimal type does not support signed zeros, infinities, or NaN's. A decimal is represented as a 96-bit integer scaled by a power of ten. For decimals with an absolute value less than 1.0m, the value is exact to the 28th decimal place, but no further. For decimals with an absolute value greater than or equal to 1.0m, the value is exact to 28 or 29 digits. Contrary to the float and double data types, decimal fractional numbers such as 0.1 can be represented exactly in the decimal representation. In the float and double representations, such numbers are often infinite fractions, making those representations more prone to round-off errors.If one of the operands of a binary operator is of type
decimal, then the other operand must be of an integral type or of type decimal. If an integral type operand is present, it is converted to decimal before the operation is performed.The result of an operation on values of type
decimal is that which would result from calculating an exact result (preserving scale, as defined for each operator) and then rounding to fit the representation. Results are rounded to the nearest representable value, and, when a result is equally close to two representable values, to the value that has an even number in the least significant digit position (this is known as "banker's rounding"). A zero result always has a sign of 0 and a scale of 0.If a decimal arithmetic operation produces a value less than or equal to
5 * 10^-29 in absolute value, the result of the operation becomes zero. If a decimal arithmetic operation produces a result that is too large for the decimal format, a System.OverflowException is thrown.The
decimal type has greater precision but smaller range than the floating-point types. Thus, conversions from the floating-point types to decimal might produce overflow exceptions, and conversions from decimal to the floating-point types might cause loss of precision. For these reasons, no implicit conversions exist between the floating-point types and decimal, and without explicit casts, it is not possible to mix floating-point and decimal operands in the same expression.The bool type
The
bool type represents boolean logical quantities. The possible values of type bool are true and false.No standard conversions exist between
bool and other types. In particular, the bool type is distinct and separate from the integral types, and a bool value cannot be used in place of an integral value, and vice versa.In the C and C++ languages, a zero integral or floating-point value, or a null pointer can be converted to the boolean value
false, and a non-zero integral or floating-point value, or a non-null pointer can be converted to the boolean value true. In C#, such conversions are accomplished by explicitly comparing an integral or floating-point value to zero, or by explicitly comparing an object reference to null.Enumeration types
An enumeration type is a distinct type with named constants. Every enumeration type has an underlying type, which must be
byte, sbyte, short, ushort, int, uint, long or ulong. The set of values of the enumeration type is the same as the set of values of the underlying type. Values of the enumeration type are not restricted to the values of the named constants. Enumeration types are defined through enumeration declarations (Enum declarations).Nullable types
A nullable type can represent all values of its underlying type plus an additional null value. A nullable type is written
T?, where T is the underlying type. This syntax is shorthand for System.Nullable<T>, and the two forms can be used interchangeably.A non-nullable value type conversely 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-nullable value type (that is, any type parameter with a struct constraint). The System.Nullable<T> type specifies the value type constraint for T (Type parameter constraints), which means that the underlying type of a nullable type can be any non-nullable value type. The underlying type of a nullable type cannot be a nullable type or a reference type. For example, int?? and string? are invalid types.An instance of a nullable type
T? has two public read-only properties:- A
HasValueproperty of typebool
- A
Valueproperty of typeT
An instance for which
HasValue is true is said to be non-null. A non-null instance contains a known value and Value returns that value.An instance for which
HasValue is false is said to be null. A null instance has an undefined value. Attempting to read the Value of a null instance causes a System.InvalidOperationException to be thrown. The process of accessing the Value property of a nullable instance is referred to as unwrapping.In addition to the default constructor, every nullable type
T? has a public constructor that takes a single argument of type T. Given a value x of type T, a constructor invocation of the formnew T?(x)creates a non-null instance of
T? for which the Value property is x. The process of creating a non-null instance of a nullable type for a given value is referred to as wrapping.Implicit conversions are available from the
null literal to T? (Null literal conversions) and from T to T? (Implicit nullable conversions).Reference types
A reference type is a class type, an interface type, an array type, or a delegate type.
reference_type
: class_type
| interface_type
| array_type
| delegate_type
;
class_type
: type_name
| 'object'
| 'dynamic'
| 'string'
;
interface_type
: type_name
;
array_type
: non_array_type rank_specifier+
;
non_array_type
: type
;
rank_specifier
: '[' dim_separator* ']'
;
dim_separator
: ','
;
delegate_type
: type_name
;A reference type value is a reference to an instance of the type, the latter known as an object. The special value
null is compatible with all reference types and indicates the absence of an instance.Class types
A class type defines a data structure that contains data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors and static constructors), and nested types. Class types support inheritance, a mechanism whereby derived classes can extend and specialize base classes. Instances of class types are created using object_creation_expressions (Object creation expressions).
Class types are described in Classes.
Certain predefined class types have special meaning in the C# language, as described in the table below.
The object type
The
object class type is the ultimate base class of all other types. Every type in C# directly or indirectly derives from the object class type.The keyword
object is simply an alias for the predefined class System.Object.The dynamic type
The
dynamic type, like object, can reference any object. When operators are applied to expressions of type dynamic, their resolution is deferred until the program is run. Thus, if the operator cannot legally be applied to the referenced object, no error is given during compilation. Instead an exception will be thrown when resolution of the operator fails at run-time.Its purpose is to allow dynamic binding, which is described in detail in Dynamic binding.
dynamic is considered identical to object except in the following respects:- Operations on expressions of type
dynamiccan be dynamically bound (Dynamic binding).
- Type inference (Type inference) will prefer
dynamicoverobjectif both are candidates.
Because of this equivalence, the following holds:
- There is an implicit identity conversion between
objectanddynamic, and between constructed types that are the same when replacingdynamicwithobject
- Implicit and explicit conversions to and from
objectalso apply to and fromdynamic.
- Method signatures that are the same when replacing
dynamicwithobjectare considered the same signature
- The type
dynamicis indistinguishable fromobjectat run-time.
- An expression of the type
dynamicis referred to as a dynamic expression.
The string type
The
string type is a sealed class type that inherits directly from object. Instances of the string class represent Unicode character strings.Values of the
string type can be written as string literals (String literals).The keyword
string is simply an alias for the predefined class System.String.Interface types
An interface defines a contract. A class or struct that implements an interface must adhere to its contract. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.
Interface types are described in Interfaces.
Array types
An array is a data structure that contains zero or more variables which are accessed through computed indices. The variables contained in an array, also called the elements of the array, are all of the same type, and this type is called the element type of the array.
Array types are described in Arrays.
Delegate types
A delegate is a data structure that refers to one or more methods. For instance methods, it also refers to their corresponding object instances.
The closest equivalent of a delegate in C or C++ is a function pointer, but whereas a function pointer can only reference static functions, a delegate can reference both static and instance methods. In the latter case, the delegate stores not only a reference to the method's entry point, but also a reference to the object instance on which to invoke the method.
Delegate types are described in Delegates.
Boxing and unboxing
The concept of boxing and unboxing is central to C#'s type system. It provides a bridge between value_types and reference_types by permitting any value of a value_type to be converted to and from type
object. Boxing and unboxing enables a unified view of the type system wherein a value of any type can ultimately be treated as an object.Boxing conversions
A boxing conversion permits a value_type to be implicitly converted to a reference_type. The following boxing conversions exist:
- From any value_type to the type
object.
- From any value_type to the type
System.ValueType.
- From any non_nullable_value_type to any interface_type implemented by the value_type.
- From any nullable_type to any interface_type implemented by the underlying type of the nullable_type.
- From any enum_type to the type
System.Enum.
- From any nullable_type with an underlying enum_type to the type
System.Enum.
- Note that an implicit conversion from a type parameter will be executed as a boxing conversion if at run-time it ends up converting from a value type to a reference type (Implicit conversions involving type parameters).
Boxing a value of a non_nullable_value_type consists of allocating an object instance and copying the non_nullable_value_type value into that instance.
Boxing a value of a nullable_type produces a null reference if it is the
null value (HasValue is false), or the result of unwrapping and boxing the underlying value otherwise.The actual process of boxing a value of a non_nullable_value_type is best explained by imagining the existence of a generic boxing class, which behaves as if it were declared as follows:
sealed class Box<T>: System.ValueType
{
T value;
public Box(T t) {
value = t;
}
}Boxing of a value
v of type T now consists of executing the expression new Box<T>(v), and returning the resulting instance as a value of type object. Thus, the statementsint i = 123;
object box = i;conceptually correspond to
int i = 123;
object box = new Box<int>(i);A boxing class like
Box<T> above doesn't actually exist and the dynamic type of a boxed value isn't actually a class type. Instead, a boxed value of type T has the dynamic type T, and a dynamic type check using the is operator can simply reference type T. For example,int i = 123;
object box = i;
if (box is int) {
Console.Write("Box contains an int");
}will output the string "
Box contains an int" on the console.A boxing conversion implies making a copy of the value being boxed. This is different from a conversion of a reference_type to type
object, in which the value continues to reference the same instance and simply is regarded as the less derived type object. For example, given the declarationstruct Point
{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}the following statements
Point p = new Point(10, 10);
object box = p;
p.x = 20;
Console.Write(((Point)box).x);will output the value 10 on the console because the implicit boxing operation that occurs in the assignment of
p to box causes the value of p to be copied. Had Point been declared a class instead, the value 20 would be output because p and box would reference the same instance.Unboxing conversions
An unboxing conversion permits a reference_type to be explicitly converted to a value_type. The following unboxing conversions exist:
- From the type
objectto any value_type.
- From the type
System.ValueTypeto any value_type.
- From any interface_type to any non_nullable_value_type that implements the interface_type.
- From any interface_type to any nullable_type whose underlying type implements the interface_type.
- From the type
System.Enumto any enum_type.
- From the type
System.Enumto any nullable_type with an underlying enum_type.
- Note that an explicit conversion to a type parameter will be executed as an unboxing conversion if at run-time it ends up converting from a reference type to a value type (Explicit dynamic conversions).
An unboxing operation to a non_nullable_value_type consists of first checking that the object instance is a boxed value of the given non_nullable_value_type, and then copying the value out of the instance.
Unboxing to a nullable_type produces the null value of the nullable_type if the source operand is
null, or the wrapped result of unboxing the object instance to the underlying type of the nullable_type otherwise.Referring to the imaginary boxing class described in the previous section, an unboxing conversion of an object
box to a value_type T consists of executing the expression ((Box<T>)box).value. Thus, the statementsobject box = 123;
int i = (int)box;conceptually correspond to
object box = new Box<int>(123);
int i = ((Box<int>)box).value;For an unboxing conversion to a given non_nullable_value_type to succeed at run-time, the value of the source operand must be a reference to a boxed value of that non_nullable_value_type. If the source operand is
null, a System.NullReferenceException is thrown. If the source operand is a reference to an incompatible object, a System.InvalidCastException is thrown.For an unboxing conversion to a given nullable_type to succeed at run-time, the value of the source operand must be either
null or a reference to a boxed value of the underlying non_nullable_value_type of the nullable_type. If the source operand is a reference to an incompatible object, a System.InvalidCastException is thrown.Constructed types
A generic type declaration, by itself, denotes an unbound generic type that is used as a "blueprint" to form many different types, by way of applying type arguments. The type arguments are written within angle brackets (
< and >) immediately following the name of the generic type. A type that includes at least one type argument is called a constructed type. A constructed type can be used in most places in the language in which a type name can appear. An unbound generic type can only be used within a typeof_expression (The typeof operator).Constructed types can also be used in expressions as simple names (Simple names) or when accessing a member (Member access).
When a namespace_or_type_name is evaluated, only generic types with the correct number of type parameters are considered. Thus, it is possible to use the same identifier to identify different types, as long as the types have different numbers of type parameters. This is useful when mixing generic and non-generic classes in the same program:
namespace Widgets
{
class Queue {...}
class Queue<TElement> {...}
}
namespace MyApplication
{
using Widgets;
class X
{
Queue q1; // Non-generic Widgets.Queue
Queue<int> q2; // Generic Widgets.Queue
}
}A type_name might identify a constructed type even though it doesn't specify type parameters directly. This can occur where a type is nested within a generic class declaration, and the instance type of the containing declaration is implicitly used for name lookup (Nested types in generic classes):
class Outer<T>
{
public class Inner {...}
public Inner i; // Type of i is Outer<T>.Inner
}In unsafe code, a constructed type cannot be used as an unmanaged_type (Pointer types).
Type arguments
Each argument in a type argument list is simply a type.
type_argument_list
: '<' type_arguments '>'
;
type_arguments
: type_argument (',' type_argument)*
;
type_argument
: type
;In unsafe code (Unsafe code), a type_argument may not be a pointer type. Each type argument must satisfy any constraints on the corresponding type parameter (Type parameter constraints).
Open and closed types
All types can be classified as either open types or closed types. An open type is a type that involves type parameters. More specifically:
- A type parameter defines an open type.
- An array type is an open type if and only if its element type is an open type.
- A constructed type is an open type if and only if one or more of its type arguments is an open type. A constructed nested type is an open type if and only if one or more of its type arguments or the type arguments of its containing type(s) is an open type.
A closed type is a type that is not an open type.
At run-time, all of the code within a generic type declaration is executed in the context of a closed constructed type that was created by applying type arguments to the generic declaration. Each type parameter within the generic type is bound to a particular run-time type. The run-time processing of all statements and expressions always occurs with closed types, and open types occur only during compile-time processing.
Each closed constructed type has its own set of static variables, which are not shared with any other closed constructed types. Since an open type does not exist at run-time, there are no static variables associated with an open type. Two closed constructed types are the same type if they are constructed from the same unbound generic type, and their corresponding type arguments are the same type.
Bound and unbound types
The term unbound type refers to a non-generic type or an unbound generic type. The term bound type refers to a non-generic type or a constructed type.
An unbound type refers to the entity declared by a type declaration. An unbound generic type is not itself a type, and cannot be used as the type of a variable, argument or return value, or as a base type. The only construct in which an unbound generic type can be referenced is the
typeof expression (The typeof operator).Satisfying constraints
Whenever a constructed type or generic method is referenced, the supplied type arguments are checked against the type parameter constraints declared on the generic type or method (Type parameter constraints). For each
where clause, the type argument A that corresponds to the named type parameter is checked against each constraint as follows:- If the constraint is a class type, an interface type, or a type parameter, let
Crepresent that constraint with the supplied type arguments substituted for any type parameters that appear in the constraint. To satisfy the constraint, it must be the case that typeAis convertible to typeCby one of the following: - An identity conversion (Identity conversion)
- An implicit reference conversion (Implicit reference conversions)
- A boxing conversion (Boxing conversions), provided that type A is a non-nullable value type.
- An implicit reference, boxing or type parameter conversion from a type parameter
AtoC.
- If the constraint is the reference type constraint (
class), the typeAmust satisfy one of the following: Ais an interface type, class type, delegate type or array type. Note thatSystem.ValueTypeandSystem.Enumare reference types that satisfy this constraint.Ais a type parameter that is known to be a reference type (Type parameter constraints).
- If the constraint is the value type constraint (
struct), the typeAmust satisfy one of the following: Ais a struct type or enum type, but not a nullable type. Note thatSystem.ValueTypeandSystem.Enumare reference types that do not satisfy this constraint.Ais a type parameter having the value type constraint (Type parameter constraints).
- If the constraint is the constructor constraint
new(), the typeAmust not beabstractand must have a public parameterless constructor. This is satisfied if one of the following is true: Ais a value type, since all value types have a public default constructor (Default constructors).Ais a type parameter having the constructor constraint (Type parameter constraints).Ais a type parameter having the value type constraint (Type parameter constraints).Ais a class that is notabstractand contains an explicitly declaredpublicconstructor with no parameters.Ais notabstractand has a default constructor (Default constructors).
A compile-time error occurs if one or more of a type parameter's constraints are not satisfied by the given type arguments.
Since type parameters are not inherited, constraints are never inherited either. In the example below,
D needs to specify the constraint on its type parameter T so that T satisfies the constraint imposed by the base class B<T>. In contrast, class E need not specify a constraint, because List<T> implements IEnumerable for any T.class B<T> where T: IEnumerable {...}
class D<T>: B<T> where T: IEnumerable {...}
class E<T>: B<List<T>> {...}Type parameters
A type parameter is an identifier designating a value type or reference type that the parameter is bound to at run-time.
type_parameter
: identifier
;Since a type parameter can be instantiated with many different actual type arguments, type parameters have slightly different operations and restrictions than other types. These include:
- A type parameter cannot be used directly to declare a base class (Base class) or interface (Variant type parameter lists).
- The rules for member lookup on type parameters depend on the constraints, if any, applied to the type parameter. They are detailed in Member lookup.
- The available conversions for a type parameter depend on the constraints, if any, applied to the type parameter. They are detailed in Implicit conversions involving type parameters and Explicit dynamic conversions.
- The literal
nullcannot be converted to a type given by a type parameter, except if the type parameter is known to be a reference type (Implicit conversions involving type parameters). However, adefaultexpression (Default value expressions) can be used instead. In addition, a value with a type given by a type parameter can be compared withnullusing==and!=(Reference type equality operators) unless the type parameter has the value type constraint.
- A
newexpression (Object creation expressions) can only be used with a type parameter if the type parameter is constrained by a constructor_constraint or the value type constraint (Type parameter constraints).
- A type parameter cannot be used anywhere within an attribute.
- A type parameter cannot be used in a member access (Member access) or type name (Namespace and type names) to identify a static member or a nested type.
- In unsafe code, a type parameter cannot be used as an unmanaged_type (Pointer types).
As a type, type parameters are purely a compile-time construct. At run-time, each type parameter is bound to a run-time type that was specified by supplying a type argument to the generic type declaration. Thus, the type of a variable declared with a type parameter will, at run-time, be a closed constructed type (Open and closed types). The run-time execution of all statements and expressions involving type parameters uses the actual type that was supplied as the type argument for that parameter.
Expression tree types
Expression trees permit lambda expressions to be represented as data structures instead of executable code. Expression trees are values of expression tree types of the form
System.Linq.Expressions.Expression<D>, where D is any delegate type. For the remainder of this specification we will refer to these types using the shorthand Expression<D>.If a conversion exists from a lambda expression to a delegate type
D, a conversion also exists to the expression tree type Expression<D>. Whereas the conversion of a lambda expression to a delegate type generates a delegate that references executable code for the lambda expression, conversion to an expression tree type creates an expression tree representation of the lambda expression.Expression trees are efficient in-memory data representations of lambda expressions and make the structure of the lambda expression transparent and explicit.
Just like a delegate type
D, Expression<D> is said to have parameter and return types, which are the same as those of D.The following example represents a lambda expression both as executable code and as an expression tree. Because a conversion exists to
Func<int,int>, a conversion also exists to Expression<Func<int,int>>:Func<int,int> del = x => x + 1; // Code
Expression<Func<int,int>> exp = x => x + 1; // DataFollowing these assignments, the delegate
del references a method that returns x + 1, and the expression tree exp references a data structure that describes the expression x => x + 1.The exact definition of the generic type
Expression<D> as well as the precise rules for constructing an expression tree when a lambda expression is converted to an expression tree type, are both outside the scope of this specification.Two things are important to make explicit:
- Not all lambda expressions can be converted to expression trees. For instance, lambda expressions with statement bodies, and lambda expressions containing assignment expressions cannot be represented. In these cases, a conversion still exists, but will fail at compile-time. These exceptions are detailed in Anonymous function conversions.
Expression<D>offers an instance methodCompilewhich produces a delegate of typeD:
Func<int,int> del2 = exp.Compile();Invoking this delegate causes the code represented by the expression tree to be executed. Thus, given the definitions above, del and del2 are equivalent, and the following two statements will have the same effect:
int i1 = del(1);
int i2 = del2(1);After executing this code,
i1 and i2 will both have the value 2.