Data Types

You already met a handful of types in the Variables lesson. Now we'll go deeper: the full family of numeric types, how text and booleans are stored, the all-important difference between value types and reference types , and how to pick the right type every single time. Choosing well here prevents whole categories of bugs later.

Learn Data Types in our free C# course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick recall.

Part of the free C# course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn

💡 Real-World Analogy

Think of data types as shipping boxes of different sizes . A tiny jewellery box ( byte ) is cheap but only fits something small (0–255). A standard parcel box ( int ) fits almost anything you'll ship day to day. A freight crate ( long ) is for the rare huge item. You could ship a ring in a freight crate, but it wastes space — and you can't cram a fridge into a jewellery box. Picking the right-sized box for the job is exactly what choosing a type is.

1. The Numeric Types

C# splits numbers into integers (no decimal point) and real numbers (with a decimal point). Within each group there are several sizes, trading memory for range or precision. You'll use int for nearly all whole numbers, double for general decimals, and decimal for money. Run the example and watch how the suffixes change the type.

📊 Numeric Type Cheat Sheet

Type

Size

Range / Precision

Use for

byte

1 byte

0 to 255

Raw bytes, colour channels

short

2 bytes

±32,767

Memory-tight small numbers

int

4 bytes

±2.1 billion

The default whole number

long

8 bytes

±9.2 quintillion

IDs, file sizes, ticks

float

~7 digits

Graphics, games (speed)

double

~15 digits

Science, averages

decimal

16 bytes

~28 digits, exact

Money & finance

2. Text (char, string) and bool

A char holds exactly one character in 'single quotes' ; a string holds any amount of text in "double quotes" . A bool is the simplest type of all — it is only ever true or false . One neat detail: a char is really just a number under the hood (its Unicode code point), which is why it can implicitly become an int .

Your turn. For each variable below, replace the ___ with the correct type keyword. The comments tell you the answer if you get stuck — but try first.

3. Value Types vs Reference Types

This is the single most important idea in the lesson. Value types (all the numbers, bool , char , and struct s) store their data directly — copying one makes an independent copy. Reference types ( string , arrays, and every class ) store a reference that points to the real data elsewhere, so copying the variable just copies the pointer, and both variables can see the same object. Misunderstanding this causes "why did my other variable change?!" bugs.

🔎 Deep Dive: defaults, limits, and overflow

Every type has a default value used when a field isn't assigned: 0 for numbers, false for bool , and null for reference types. You can read it with default(int) . Each numeric type also exposes its limits via int.MinValue and int.MaxValue .

Push a value past int.MaxValue and it silently overflows (wraps to a negative number) by default. Wrap arithmetic in a checked { ... } block to make overflow throw an exception instead of failing silently.

🧩 Reorder Challenge

These five lines were shuffled. Put them in the order that compiles and prints Total: £40.00 . Think about what must exist before it can be used.

Why: a variable must be declared before it is read. total depends on both qty and unitPrice , so those two must come first. The WriteLine uses total , so it must come last. Note int * decimal safely promotes to decimal , which is why the multiplication is allowed.

🧠 Quick Recall

65 — a char implicitly converts to its Unicode code point, and 'A' is 65.

5 20 — int is a value type, so y got an independent copy. Changing y never touches x .

False then 0 — the default for bool is false and for int is 0 .

Pro Tips

Common Errors (and the fix)

Frequently Asked Questions

Mini-Challenge: Product Line

Model a product using the right type for every field. The outline is in the comments — declare the variables, compute inStock from stockCount , and print the two-line summary.

🎉 Lesson Complete

Practice quiz

Which integer type is the recommended default for whole numbers?

  • byte
  • short
  • int
  • long

Answer: int. int (4 bytes, about ±2.1 billion) is the natural default for whole numbers on modern hardware.

What is the default value of an int field?

  • 0
  • null
  • -1
  • 1

Answer: 0. Numeric types default to 0; default(int) is 0.

What is the default value of a bool?

  • true
  • 0
  • null
  • false

Answer: false. A bool defaults to false; default(bool) prints False.

Which group does string belong to?

  • Value type
  • Reference type
  • Numeric type
  • Enum

Answer: Reference type. string is a class, so it is a reference type — but it is immutable, which makes it feel value-like.

Copying a value type (like int b = a;) does what?

  • Copies the data into an independent variable
  • Copies a pointer so both share data
  • Throws an error
  • Makes b reference a

Answer: Copies the data into an independent variable. Value types copy the data, so b is independent — changing b never affects a.

After int[] second = first; changing second[0] also changes first[0]. Why?

  • Arrays are value types
  • first was copied
  • Both variables reference the same array on the heap
  • second is read-only

Answer: Both variables reference the same array on the heap. Arrays are reference types, so the assignment copies the pointer — both names point at one array.

What is the largest value a byte can hold?

  • 127
  • 255
  • 1000
  • 65535

Answer: 255. A byte holds 0 to 255 (one byte).

What is the result of int n = 'A'; where 'A' has code point 65?

  • It won't compile
  • 0
  • An 'A' character
  • 65

Answer: 65. A char implicitly converts to its Unicode code point, so n becomes 65.

Which type is best for an exact bank balance to the penny?

  • double
  • decimal
  • float
  • int

Answer: decimal. decimal stores base-10 amounts exactly, so it is correct for money; double can drift.

What do underscores in a literal like 1_500_000 do?

  • Multiply the number
  • Act as decimal points
  • Are visual separators that are ignored
  • Cause a compile error

Answer: Are visual separators that are ignored. Underscores are purely visual digit separators and are ignored by the compiler.

Continue this course