Data types in C #

Source: Internet
Author: User
Tags float range

One/value type

1/integral type

    • C # supports 9 integer types: sbyte, Byte, short, ushort, int, uint, long, ulong, and Char. An integral type has the size and range of values listed below:
    • The sbyte type represents a signed 8-bit integer with a value between 128 and 127.
    • The byte type represents an unsigned 8-bit integer with a value between 0 and 255.
    • The short type represents a signed 16-bit integer with a value between 32768 and 32767.
    • The ushort type represents an unsigned 16-bit integer with a value between 0 and 65535.
    • The int type represents a signed 32-bit integer with a value between 2147483648 and 2147483647.
    • The uint type represents an unsigned 32-bit integer with a value between 0 and 4294967295.
    • A long type represents a signed 64-bit integer with a value between 9223372036854775808 and 9223372036854775807.
    • The ulong type represents an unsigned 64-bit integer with a value between 0 and 18446744073709551615.
    • The char type represents an unsigned 16-bit integer with a value between 0 and 65535. The set of possible values for the char type corresponds to the Unicode character set. Although the representation of Char is the same as ushort, all calculations that can be made on one type may not be performed on another type.

The integer unary operator and the two-tuple operator are always evaluated for signed 32-bit precision, unsigned 32-bit precision, signed 64-bit precision, or unsigned 64-bit precision:

    • For unary operators + and ~, the operands are converted to type T, where T is the first type in int, uint, long, and ulong that can fully represent all possible values of the operand. The operation is then performed with the precision of type T, and the type of the result is type T.
    • For unary operators – the operands are converted to type T, where T is the first type in int and long that can fully represent all possible values of the operand. The operation is then performed with the precision of type T, and the type of the result is type T. Unary operator – cannot be applied to operands of type ulong.
    • For +, –, *,/,%, &, ^, |, = =,! =, >, <, >=, and <= two-tuple operators, the operands are converted to type T, where T is the first of the int, uint, long, and ulong that can fully represent all possible values of two operands. Type. The operation is then performed with the precision of the t type, and the type of the result of the operation belongs to T (for the relational operator is bool). For a two-tuple operator, one operand is not allowed to be of type long and the other operand is a ulong type.
    • For the two-tuple operator << and >>, the left operand is converted to type T, where T is the first type in int, uint, long, and ulong that can fully represent all possible values of the operand. The operation is then performed with the precision of type T, and the type of the result is type T.

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

    • There is no implicit conversion from other types to char types. Specifically, there is no implicit conversion from sbyte, Byte, or ushort to char, even if the sbyte, byte, and ushort types have a range of values that can be represented entirely by the char type.
    • Constants of type char must be written as Character-literal or integer-literal with cast to type char. For example, (char) 10 is the same as ' \x000a '.

The checked and unchecked operators and statements are used to control overflow checking of integer arithmetic operations and conversions. In the checked context, an overflow generates a compile-time error or causes a System.OverflowException to be raised. Overflows are ignored in the unchecked context, and any high-order bits that do not match the target type are discarded.

2/floating-point

C # supports two floating-point types: float and double. float and double types are represented by 32-bit single-precision and 64-bit double-precision IEEE 754 formats that provide the following sets of values:

    • Positive zero and negative 0. In most cases, positive and negative zeros behave the same as simple values of 0, but some operations discriminate between the two 0.
    • Positive infinity large and negative infinity. Infinity is generated by a number of non-0 numbers that are 0 apart from such operations. For example, 1.0/0.0 produces positive infinity, while –1.0/0.0 produces negative infinity.
    • Non-digital (not-a-number) value, often abbreviated as NaN. NaN is produced by an invalid floating-point operation (such as 0 being removed by 0).
    • A finite set of non-0 values expressed in sxmx2e form, where S is 1 or? 1,m and e are determined by a special floating-point type: For float, 0 < m < 224 and? 149≤e≤104 for double, 0 < M < 253 and 1075≤e≤970. A non-normalized floating-point number is considered a valid non-0 value.

The float type can represent values with a precision of 7 bits and a range of approximately 1.5x10?45 to 3.4x1038.

A double type can represent a value that is 15-bit or 16-bit, within the range of approximately 5.0x10?324 to 1.7x10308.

If one operand of the two-tuple operator is floating-point, the other operand must be an integer or floating-point type, and the operation is computed as follows:

    • If one operand is an integer, the operand is converted to the same float type as the other operand.
      • Then, if the type of either operand is double, the other operand is converted to double. The operation is performed at least with a double range and precision, and the result is of type double (or bool for the relational operator).
      • Otherwise, the operation is performed at least with the float range and precision, and the result is of type float (or bool for the relational operator).

Floating-point operators (including assignment operators) never produce an exception. Conversely, in exceptional cases, floating-point operations produce 0, 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 either positive zero or negative 0.
    • If the result of a floating-point operation is too large for the destination format, the result of the operation becomes either positive infinity or negative infinity.
      • If the floating-point operation is invalid, the result of the operation becomes NaN.
      • If one or two operands of a floating-point operation are Nan, the result of the operation becomes Nan.

Floating-point arithmetic can be performed with a higher precision than the result type of the operation. For example, some hardware architectures support "extended" or "long double" floating-point types that have a greater range and precision than double types and implicitly use this higher precision type to perform all floating-point operations. Only the performance overhead is too high for such a hardware structure to perform floating-point operations with a "low" precision. C # takes a higher precision type for all floating-point operations, rather than enforcing the specified precision, resulting in loss of performance and precision at the same time. In addition to passing more accurate results, this rarely produces any perceptible effect. However, in an expression of the form x * y/z, where the multiplication results in a result that exceeds the double range, and the subsequent division causes the temporary result to return to the double range, the expression is evaluated in a larger format, which may result in a finite value (which should have been infinite).

3/decimal type

The decimal type is a 128-bit data type that is suitable for use in financial calculations and currency calculations. The decimal type can represent a value that has 28 or 29 significant digits, from 1.0x10?28 to approximately 7.9x1028 range.

The finite value set of the decimal type is in the form (–1) sxcx10-e, where the symbol s is 0 or 1, the coefficient c is given by 0≤c < 296, and the number of decimal digits e satisfies the 0≤e≤28. The decimal type does not support signed 0, Infinity, or NaN. A decimal can be represented by a 96-bit integer represented by a power of 10. For a decimal with an absolute value less than 1.0m, its value is up to the 28th decimal place. For a decimal with an absolute value greater than or equal to 1.0m, it is accurate to the 28th or 29 digits after the decimal point. In contrast to the float and double data types, decimal decimal numbers (such as 0.1) can be represented precisely in decimal notation. In the float and double representations, such numbers usually become infinite decimals, making these representations more prone to rounding errors.

If one operand of the two-tuple operator is of type Decimal, the other operand must be an integer or decimal type. If there is an integer operand, it is converted to decimal before the operation is performed.

The result of the operation of the decimal type value is that an exact result is computed (the number of decimal digits is retained by the definition of each operator), and then rounded to fit the representation. The result is rounded to the nearest possible value, and when the result is similarly close to two values that can be represented, the value that is even in the position of the least significant number of digits is rounded (this is called banker rounding). 0 The result always contains the symbol 0 and the number of decimal digits 0.

If a decimal arithmetic operation produces a value that has an absolute value less than or equal to 5x10-29, the result of the operation becomes 0. If the value produced by the decimal arithmetic operation is too large for the decimal format, System.OverflowException is raised.

The decimal type has a higher precision than a floating-point type, but the range of values is small. Therefore, conversions from floating-point to decimal can produce overflow exceptions, while conversions from decimal to floating-point types can result in a loss of precision. For these reasons, there is no implicit conversion between floating-point and decimal, and it is not possible to use both floating-point operands and decimal operands in the same expression without explicitly imposing casts.

4/bool type

The bool type represents the Boolean logical amount. Possible values of type bool are true and false.

There is no standard conversion between bool and other types. Specifically, the bool type is quite different from the integer type, and the bool value cannot be used in place of an integer value or vice versa.

In the C and C + + languages, a 0 integer or floating-point value or a null pointer can be converted to a Boolean value of false, a nonzero integer or floating-point value, or a non-null pointer can be converted to a Boolean value of true. In C #, this conversion is done by explicitly comparing an integer or floating-point value to 0, or by explicitly comparing an object reference to null.

5/enum type

Enumeration types are unique types that have named constants. Each enumeration type has an underlying type that must be a byte, sbyte, short, ushort, int, uint, long, or ulong. The value set of the enumeration type is the same as the set of values for its underlying type. The value of the enumeration type is not limited to the values of those named constants. An enumeration type is defined by an enumeration declaration.

Data types in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.