C# commonly used data types, usage, differences, sizes

  1. Integer Types:

    • int: Used to store integer values, usually occupies 4 bytes, and the value range is -2,147,483,648 to 2,147,483,647.
    • long: Used to store large integer values, usually occupies 8 bytes, and has a wider value range.
  2. Floating-Point Types:

    • float: Used to store single-precision floating point numbers, usually occupying 4 bytes.
    • double: Used to store double-precision floating point numbers, usually occupying 8 bytes.
  3. Character Type:

    • char: Used to store a single character, occupying 2 bytes.
  4. Boolean Type:

    • bool: A Boolean value used to represent true or false, occupying 1 byte.
  5. String Type:

    • string: Used to store text strings. The memory occupied depends on the string length and encoding method.
  6. Date and Time Types:

    • DateTime: Used to store date and time information, occupying 8 bytes.
    • TimeSpan: Used to represent the time interval, occupying 8 bytes.
  7. Enum Type:

    • enum: Used to define a set of named constant values, usually based on integer type, so it takes up 4 bytes.
  8. Array Types:

    • T[]: Used to store a group of elements of the same type. The memory occupied depends on the array length and element type.
  9. Null Type:

    • null: Represents a null reference or missing value, which does not occupy memory space.
  10. Dynamic Type:

    • dynamic: The type is determined at runtime, and the memory space occupied depends on its actual type.

Note that the sizes of data types may vary depending on computer architecture and compiler; the above sizes are generally standard. Choosing the appropriate data type depends on your program needs and memory efficiency considerations.

Guess you like

Origin blog.csdn.net/Steel_nails/article/details/133671416