C# Data Types

C# provides a variety of data types to store different kinds of values. Data types are categorized into value types and reference types. Here's a list of common C# data types:

Value Types

  • Integral Types:
    • byte: 8-bit unsigned integer (0 to 255)
    • sbyte: 8-bit signed integer (-128 to 127)
    • short: 16-bit signed integer (-32,768 to 32,767)
    • ushort: 16-bit unsigned integer (0 to 65,535)
    • int: 32-bit signed integer (-2,147,483,648 to 2,147,483,647)
    • uint: 32-bit unsigned integer (0 to 4,294,967,295)
    • long: 64-bit signed integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
    • ulong: 64-bit unsigned integer (0 to 18,446,744,073,709,551,615)
  • Floating-point Types:
    • float: 32-bit single-precision floating-point number (~6-7 digits precision)
    • double: 64-bit double-precision floating-point number (~15-16 digits precision)
  • High Precision:
    • decimal: 128-bit high-precision floating-point number (28-29 significant digits)
  • Boolean Type: bool (true or false)
  • Character Type: char (single Unicode character)

Reference Types

  • string: Represents a sequence of characters (text).
  • object: Base type for all types in C#. Any data type can be assigned to object.
  • dynamic: Determines the type at runtime, allowing dynamic typing.

Nullable Types

  • Nullable types allow value types to hold null. Example: int?, float?

Enumeration Types (Enum)

  • enum: Represents a set of named constants. Example: enum Day { Sunday, Monday }

Tuple Types

  • ValueTuple: Lightweight tuples to store multiple values. Example: (int, string) myTuple = (1, "Hello")

Pointer Types (Unsafe Context)

  • Pointers (int*, char*, etc.): Used in unsafe code for direct memory manipulation.

Summary

C# provides various data types to represent different forms of data, from simple integers and floating-point numbers to complex reference types and enumerations. Understanding the data types helps you efficiently allocate memory and perform type-specific operations in your programs.