C# Data Type

Nourhan IbrahimNourhan Ibrahim
2 min read

In C#, data types define the kind of values a variable can hold and how much memory it uses. Since C# is a strongly typed language, every variable must have a specific type declared before it is used. Understanding data types is one of the most important steps in learning C#.

Categories of Data Types in C

C# data types are divided into several categories:

  1. Value Types

  2. Reference Types

  3. Pointer Types (used in unsafe code)

For beginners, the most important categories are value types and reference types.


Value Types

Value types store the actual data directly in memory. When you assign one value type to another, the value is copied.

Examples of value types in C#:

  • int → Stores whole numbers. Example: 10

  • double → Stores double-precision floating point numbers. Example: 3.14

  • float → Stores single-precision floating point numbers. Example: 2.5f

  • decimal → Stores high-precision decimal numbers, useful for financial calculations. Example: 19.99m

  • char → Stores a single character. Example: 'A'

  • bool → Stores true or false values. Example: true

  • byte → Stores unsigned 8-bit numbers (0 to 255). Example: 200

  • short → Stores 16-bit integers. Example: -32768 to 32767

  • long → Stores 64-bit integers. Example: 9223372036854775807


Reference Types

Reference types store a reference (or address) to the actual data, not the data itself. When assigning one reference type variable to another, both refer to the same memory location.

Examples of reference types:

  • string → Stores text. Example: "Hello, World!"

  • object → The base type from which all other types derive.

  • arrays → A collection of elements of the same type. Example: numbers = {1, 2, 3, 4}

  • classes → User-defined types that represent objects with fields and methods.


Null Value

Reference types can be null, meaning they do not point to any object. Value types normally cannot be null, but you can make them nullable by adding a question mark. Example: int? number = null


Type Conversion

Sometimes, you need to convert one data type into another. C# provides two ways:

  1. Implicit Conversion (safe, automatic)
    Example: int x = 10; double y = x;

  2. Explicit Conversion (requires casting)
    Example: double a = 9.8; int b = (int)a;

C# also provides helper methods like Convert.ToInt32() or int.Parse() for conversions.


Default Values

Every data type in C# has a default value when not explicitly assigned:

  • int → 0

  • double → 0.0

  • bool → false

  • string → null

  • object → null

0
Subscribe to my newsletter

Read articles from Nourhan Ibrahim directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nourhan Ibrahim
Nourhan Ibrahim

Full Stack Engineer | Product Designer Electronics Engineering Student Taekwondo Player🥋 https://linktr.ee/nouribram