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
  1. Numeric: signed integer (sbyte, short, int, long), unsigned (byte, ushort, uint, ulong), real number (float, double, decimal)
  2. Logical (bool)
  3. 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

  1. Integral – signed consists of sbyte, short, int, long
  2. Integral – unsigned consists of byte, ushort, uint, ulong
  3. Real consists of float, double, decimal

Literals

  1. Numeric Literals: Integral literals – decimal or hexadecimal denoted with 0x prefix
    int x = 127;
    long y = 0x7F;
    
  2. 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

  1. Integral to integral conversions
    Implicit when destination can represent every possible value or source type otherwise explicit
  2. Floating-point to floating-point conversions
    Float to double implicit, otherwise explicit
  3. Floating-point to integral conversions
    Implicit conversion for all integral types to floating, reverse is explicit
  4. 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

  1. Integral division

    Division on integral always truncate remainders (round towards zero)

    Dividing by literal or zero generates compile-time error

  2. Integral overflow

    At run-time, arithmetic operations on integral types can overflow

  3. 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.
    
  4. Overflow checking for constant expressions

    Expressions evaluated at compile time are overflow-checked unless you apply unchecked operator

  5. Bitwise operators

    ~ Complement

    & And

    | Or

    ^ Exclusive Or

    << Shift left

    >> Shift right

  6. 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

  7. 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

  1. System.Boolean and logical assigment is true or false
  2. Default value is false
  3. Default value for bool? is null
  4. Requires one bit of storage but run time uses byte of memory
  5. Alternative is to use BitArray in System.Collections namespace, it uses 1 bit per boolean value
  6. No casting available from bool to numeric and vice versa
Byte

  1. System.Byte
  2. Can declare by assigning decimal literal, hexadecimal literal or binary literal
  3. Unsigned 8-bit integer range from 0 to 255
Char

  1. System.Char – occupies 2 bytes, uses single quotes
  2. Unicode 16-bit character
  3. Can be implicitly converted to ushort, int, uint, long, ulong, float, double, decimal.
  4. No conversion from other types to char
  5. Has static method Reference
  6. Escape sequences express characters that cannot be expressed or interpreted literally.
  7. \u or \x lets you specify any Unicode character via 4-digit hexadeciaml code:
    char copyrightSymbol = '\u00A9';
    char omegaSymbol = '\u03A9';
    char newLine = '\u000A';
    
Char Meaning Value
\’ Single Quote 0x0027
\” Double Quote 0x0022
\\ Backslash 0x005C
\0 Null 0x0000
\a Alert 0x0007
\b Backspace 0x0008
\f Form feed 0x000C
\n New line 0x000A
\r Carriage return 0x000D
\t Horizontal tab 0x0009
\v Vertical tab 0x000B
decimal

  1. System.Decimal
  2. 128-bit data type with more precision and smaller range
  3. default value is 0m
  4. use suffix m or M to be treated as decimal otherwise will be treated as double
  5. Integral types can be implicitly converted otherwise you will need to cast
  6. use String.Format with standard currency format:
    Console.WriteLine("Your amount = {0:C}", y);

Leave a Reply

Your email address will not be published. Required fields are marked *