CATEGORII DOCUMENTE 
loading...

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, and are further subdivided into numeric types, integral types, and floatingpoint types.


valuetype:
structtype
enumtype
structtype:
typename
simpletype
simpletype:
numerictype
bool
numerictype:
integraltype
floatingpointtype
decimal
integraltype:
sbyte
byte
short
ushort
int
uint
long
ulong
char
floatingpointtype:
float
double
enumtype:
typename
All value types implicitly inherit from class object. It is not possible for any type to derive from a value type, and value types are thus implicitly sealed (§10.1.1.2).
A variable of a value type always contains a value of that type. Unlike reference types, it is not possible for a value of a value type to be null or to reference an object of a more derived type.
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.
All value types implicitly declare a public parameterless instance constructor called the default constructor. The default constructor returns a zeroinitialized instance known as the default value for the value type:
For all simpletypes, the default value is the value produced by a bit pattern of all zeros:
For sbyte, byte, short, ushort, int, uint, long, and ulong, the default value is 0.
For char, the default value is 'x0000'.
For float, the default value is 0.0f.
For double, the default value is 0.0d.
For decimal, the default value is 0.0m.
For bool, the default value is false.
For an enumtype E, the default value is 0.
For a structtype, the default value is the value produced by setting all value type fields to their default value and all reference type fields to null.
Like any other instance constructor, the default constructor of a value type is invoked using the new operator. (Note: for efficiency reasons, this requirement is not intended to actually have the implementation generate a constructor call.) In the example below, the variables i and j are both initialized to zero.
class A
}
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 (§11.3.8). For example
struct
Point
}
Given the above declaration, the statements
Point p1
= new Point();
Point p2 = new Point(0, 0);
both create a Point with x and y initialized to zero.
A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types. Struct types are described in §11.
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 
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 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 method
The 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 (§2.4.4). For example, 123 is a literal of type int and 'a' is a literal of type char. C# makes no provision for literals of other struct types, and 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 compiletime. Such an expression is known as a constantexpression (§7.15). Expressions involving operators defined by other struct types are not considered to be constant expressions.
Through const declarations it is possible to declare constants of the simple types (§10.3). It is not possible to have constants of other struct types, but a similar effect is provided by static readonly fields.
Conversions involving simple types can participate in evaluation of conversion operators defined by other struct types, but a userdefined conversion operator can never participate in evaluation of another userdefined operator (§6.4.2).
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 sbyte type represents signed 8bit integers with values between –128 and 127.
The byte type represents unsigned 8bit integers with values between 0 and 255.
The short type represents signed 16bit integers with values between –32768 and 32767.
The ushort type represents unsigned 16bit integers with values between 0 and 65535.
The int type represents signed 32bit integers with values between –2147483648 and 2147483647.
The uint type represents unsigned 32bit integers with values between 0 and 4294967295.
The long type represents signed 64bit integers with values between –9223372036854775808 and 9223372036854775807.
The ulong type represents unsigned 64bit integers with values between 0 and 18446744073709551615.
The char type represents unsigned 16bit integers with values between 0 to 65535. The set of possible values for the char type corresponds to the Unicode character set. Although char has the same representation as ushort, not all operations permitted on one type are permitted on the other.
The integraltype unary and binary operators always operate with signed 32bit precision, unsigned 32bit precision, signed 64bit precision, or unsigned 64bit precision:
For the unary + and ~ operators, the operand is converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of the operand. The operation is then performed using the precision of type T, and the type of the result is T.
For the unary—operator, the operand is converted to type T, where T is the first of int and long that can fully represent all possible values of the operand. The operation is then performed using the precision of type T, and the type of the result is T. The unary—operator cannot be applied to operands of type ulong.
For the binary +, –, *, /, %, &, ^, , ==, !=, >, <, >=, and <= operators, the operands are converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of both operands. The operation is then performed using the precision of type T, and the type of the result is T (or bool for the relational operators). It is not possible for one operand to be of type long and the other to be of type ulong with the binary operators.
For the binary << and >> operators, the left operand is converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of the operand. The operation is then performed using the precision of type T, and the type of the result is T.
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 char type. In particular, even though the sbyte, byte, and ushort types have ranges of values that are fully representable using the char type, implicit conversions from sbyte, byte, or ushort to char do not exist.
Constants of the char type must be written as characterliterals. Character constants can only be written as integerliterals in combination with a cast. For example, (char)10 is the same as 'x000A'.
The checked and unchecked operators and statements are used to control overflow checking for integraltype arithmetic operations and conversions (§7.5.12). In a checked context, an overflow produces a compiletime error or causes a System.OverflowException to be thrown. In an unchecked context, overflows are ignored and any highorder bits that do not fit in the destination type are discarded.
C# supports two floating point types: float and double. The float and double types are represented using the 32bit singleprecision and 64bit doubleprecision 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.
Positive infinity and negative infinity. Infinities are produced by such operations as dividing a nonzero number by zero. For example 1.0 / 0.0 yields positive infinity, and –1.0 / 0.0 yields negative infinity.
The NotaNumber value, often abbreviated
The finite set of nonzero values of the form s × m × 2^{e}, where s is 1 or −1, and m and e are determined by the particular floatingpoint type: For float, 0 < m < 2^{24} and −149 ≤ e ≤ 104, and for double, 0 < m < 2^{53} and −1075 ≤ e ≤ 970. Denormalized floatingpoint numbers are considered valid nonzero 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 1516 digits.
If one of the operands of a binary operator is of a floatingpoint type, then the other operand must be of an integral type or a floatingpoint 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 floatingpoint type of the other operand.
Then, if either of the operands is of type double, the other operand is converted to double, the operation is performed using at least double range and precision, and the type of the result is double (or bool for the relational operators).
Otherwise, the operation is performed using at least float range and precision, and the type of the result is float (or bool for the relational operators).
The
floatingpoint operators, including the assignment operators, never produce
exceptions. Instead, in exceptional situations, floatingpoint operations
produce zero, infinity, or
If the result of a floatingpoint operation is too small for the destination format, the result of the operation becomes positive zero or negative zero.
If the result of a floatingpoint operation is too large for the destination format, the result of the operation becomes positive infinity or negative infinity.
If a
floatingpoint operation is invalid, the result of the operation becomes
If one
or both operands of a floatingpoint operation is NaN,
the result of the operation becomes
Floatingpoint 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” floatingpoint type with greater range and precision than the double type, and implicitly perform all floatingpoint operations using this higher precision type. Only at excessive cost in performance can such hardware architectures be made to perform floatingpoint 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 floatingpoint 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 is a 128bit 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 2829 significant digits.
The
finite set of values of type decimal are of the form s
× m × 10^{e}, where s is
1 or –1, 0 ≤ m < 2^{96},
and −28 ≤ e ≤ 0.
The decimal type does not support signed zeros, infinities, or
A decimal is represented as a 96bit integer scaled by a power of ten. For decimals with an absolute value less than 1.0m, the value is exact to the 28^{th} 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 roundoff 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.
Operations on values of type decimal are exact to 28 or 29 digits, but to no more than 28 decimal places. 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.
If a decimal arithmetic operation produces a value that is too small for the decimal format after rounding, 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 floatingpoint types. Thus, conversions from the floatingpoint types to decimal might produce overflow exceptions, and conversions from decimal to the floatingpoint types might cause loss of precision. For these reasons, no implicit conversions exist between the floatingpoint types and decimal, and without explicit casts, it is not possible to mix floatingpoint and decimal operands in the same expression.
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, nor vice versa.
In the C and C++ languages, a zero integral value or a null pointer can be converted to the boolean value false, and a nonzero integral value or a nonnull pointer can be converted to the boolean value true. In C#, such conversions are accomplished by explicitly comparing an integral value to zero or explicitly comparing an object reference to null.
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. Enumeration types are defined through enumeration declarations (§14.1).
loading...

Vizualizari: 499
Importanta:
Termeni si conditii de utilizare  Contact
© SCRIGROUP 2020 . All rights reserved
Distribuie URL
Adauga cod HTML in site