Types
- A type defines the blueprint for a value
- A variable denotes the storage location which can change overtime
- A constant always represents the same value
- Predefined types are types are supported by the compiler such as int and string
- The predefined bool type has exactly two possible values: true and false.
There are two main types: value type and reference Type. Other two are generic type parameters and pointer types.
Value Types
- They are built-in types
- Content is just a simple value
- Assignment simply copies the instance which have independent memory storage
- Cannot have null value
- Has more storage overhead
- Numeric: signed integer (sbyte, short, int, long), unsigned (byte, ushort, uint, ulong), real number (float, double, decimal)
- Logical (bool)
- Character (char)
Primitive Types
They are predefined value types such as Integer excluding decimal and they are directly supported by the compiled code.
Numeric Types
- Integral – signed consists of sbyte, short, int, long
- Integral – unsigned consists of byte, ushort, uint, ulong
- Real consists of float, double, decimal
Literals
- Numeric Literals: Integral literals – decimal or hexadecimal denoted with 0x prefix
int x = 127; long y = 0x7F;
- Real literals – decimal or exponential
double d = 1.5; double million = 1E06;
Numeric literal type inference
Default compiler infers numeric literal to be double or integral type. If it has decimal point or exponential it is double or literals in order of: int, uint, long, ulong.
Numeric Suffixes
Define the type of literal, it can be either lower or upper
F | float |
float f = 1.0F; |
D | double |
double d = 1D; |
M | decimal |
decimal d = 1.0M; |
U | uint |
uint i = 1U; |
L | long |
long i = 1L; |
UL | ulong |
ulong i = 1UL; |
From this above list decimal and float are most important.
Numerical Conversions
- Integral to integral conversions
Implicit when destination can represent every possible value or source type otherwise explicit - Floating-point to floating-point conversions
Float to double implicit, otherwise explicit - Floating-point to integral conversions
Implicit conversion for all integral types to floating, reverse is explicit - Decimal conversions
All integral types can be implicitly converted to decimal
Floating-point types
Are real number types: float and double used for scientific and graphical calculations
Decimal types
Used for financial calculations – base-10 and high precision
Arithmetic Operators
The arithmetic operators (+, -, *, /, %) are defined for all numeric types except the 8-
and 16-bit integral types.
Increment and Decrement Operators
increment (++) or decrement (–) operators used to add or subtract numeric types by 1.
Specialized Integral Operations
- Integral division
Division on integral always truncate remainders (round towards zero)
Dividing by literal or zero generates compile-time error
- Integral overflow
At run-time, arithmetic operations on integral types can overflow
- Integral arithmetic overflow check operators
Checked operator generates the OverflowException for the following: ++, –, + –
Has no effect on double, float and decimal
Example int a = 1000000; int b = 1000000; int c = checked (a * b); // Checks just the expression.
- Overflow checking for constant expressions
Expressions evaluated at compile time are overflow-checked unless you apply unchecked operator
- Bitwise operators
~ Complement
& And
| Or
^ Exclusive Or
<< Shift left
>> Shift right
- 8- and 16-Bit Integrals
The 8- and 16-bit integral types are byte, sbyte, short, and ushort lack thier own arithmetic operators so implicit conversion is required
- Special Float and Double Values
NaN double.NaN float.NaN +∞ double.PositiveInfinity float.PositiveInfinity -∞ double.NegativeInfinity float.NegativeInfinity -0 -0.0 -0.0f
Dividing nonzero number by zero = infinite value
Dividing zero by zero or subtract infinity from infinity = NaN
To test value is NaN
Float.IsNaN
double.IsNaN
Note: two NaN is equal in object
double Versus decimal
- double useful for scientific computation
- decimal financial calculations
Real-Number Rounding Errors
- float and double represent base 2
- decimal base 10
Bool
|
||||||||||||||||||||||||||||||||||||
Byte
|
||||||||||||||||||||||||||||||||||||
Char
|
||||||||||||||||||||||||||||||||||||
decimal
|
||||||||||||||||||||||||||||||||||||