How Does C# Represent Numbers?
Before we can compute with numbers, we need to know how to store them. C# provides different types for different kinds of numeric data.
Integers
Whole numbers appear constantly in programming: counting items, tracking scores, measuring discrete quantities. C# represents these with the int type.
int count = 42;This creates an integer variable named count and binds the value 42 to it.
The pattern is identical to booleans. Only the type keyword changes: int instead of bool.
Integer: An integer is a 32-bit whole number which can be any value between -2³¹ and 2³¹ - 1.
Where do these bounds come from? Remember from Chapter 1 that a truth table for three variables has 2³ = 8 rows. Each variable can be true or false, and we make that choice three times. The same principle applies to bits in memory.
An int uses 32 bits. Each bit can be 0 or 1, and we make that choice 32 times. That gives us 2³² possible values, roughly 4.3 billion. C# centers this range around zero, giving us -2³¹ (-2,147,483,648) to 2³¹ - 1 (2,147,483,647).
You can access these bounds directly:
Console.WriteLine(int.MinValue); // -2147483648
Console.WriteLine(int.MaxValue); // 2147483647For our purposes, this range is more than enough.
The Mechanism
What happens when int count = 42; executes? Three discrete actions:
int— reserve space in memory for an integer valuecount— name that space “count”= 42;— bind the value 42 to that name
This is the same mechanism we saw with booleans. The computer finds room in memory, labels it with a name, and stores your value there.
Try it yourself.
Translate this code to English:
int score = 100;Write your answer before revealing ours.
Reveal answer
“Create an integer variable named score and bind the value 100 to it.”
If your answer differed, note what you missed before continuing.
Try it yourself.
Write C# code for this description:
“Create an integer variable named quantity and bind the value 50 to it.”
Reveal answer
int quantity = 50;If your answer differed, note what you missed before continuing.
Doubles
Not all numbers are whole. When we need decimals, we use a different type:
double temperature = 98.6;This creates a double variable named temperature and binds the value 98.6 to it.
Double: A double is a 64-bit floating-point number used to approximate real numbers. It has a precision of 15-16 significant digits.
The mechanism is identical to integers. When double temperature = 98.6; executes:
double— reserve space in memory for a double valuetemperature— name that space “temperature”= 98.6;— bind the value 98.6 to that name
Doubles can hold very large and very small values, far beyond the range of integers. But the word “approximate” in the definition matters. A double stores values using a fixed number of digits. When you write a number that needs more precision than 15-16 digits, the computer stores an approximation.
What does this mean in practice? Consider a number with 20 digits:
double big = 12345678901234567890.0;
Console.WriteLine(big);Output: 1.2345678901234567E+19
The last few digits changed. For everyday calculations, 15-16 digits of precision is plenty. Very large or very precise numbers may lose accuracy in the trailing digits.
Try it yourself.
Translate this code to English:
double price = 19.99;Write your answer before revealing ours.
Reveal answer
“Create a double variable named price and bind the value 19.99 to it.”
If your answer differed, note what you missed before continuing.
Try it yourself.
Write C# code for this description:
“Create a double variable named rate and bind the value 0.05 to it.”
Reveal answer
double rate = 0.05;If your answer differed, note what you missed before continuing.
Choosing Between int and double
We now have two numeric types. When should you use each?
Ask yourself: does this value ever have a decimal part?
Use int for:
- Counting things: number of items, iterations, people
- Positions: first, second, third in a sequence
- Discrete quantities: age in years, floors in a building
Use double for:
- Measurements: temperature, distance, weight
- Calculations that produce fractions: averages, percentages
- Prices and money amounts
The choice depends on what the number represents, not just what value you are storing right now. A variable counting people should be an int. You might start with 1, but you will never have 1.5 people.
Try it yourself.
For each value, decide whether int or double is more appropriate:
- Number of pages in a book
- Weight of a package in pounds
- Number of login attempts
- Temperature in Celsius
- A student’s GPA
Write your answers before revealing ours.
Reveal answer
- Number of pages → int (pages are whole numbers)
- Weight in pounds → double (weight can be 2.5 lbs)
- Login attempts → int (attempts are counted)
- Temperature → double (can be 20.5°C)
- GPA → double (can be 3.75)
If any answers differed, note your reasoning and compare it to ours.
Type Constraints
Types constrain what values are valid. Consider:
int x = 3.5;This code produces an error. Before your program runs, C# checks that your code follows the rules. This checking step is called compiling. The compiler sees that 3.5 is not a valid integer (integers cannot hold decimal parts) and rejects the assignment.
What about the other direction?
double y = 3;This works fine. A double can hold 3. It stores it as 3.0. No information is lost when storing a whole number in a double.
The pattern:
- int → double: always works (no information lost)
- double → int: fails at compile time (decimal part would be lost)
The compiler enforces these constraints before your program runs. This protects you from silently losing data.
Try it yourself.
For each line, predict whether it compiles successfully:
int a = 100;
int b = -50;
int c = 7.0;
double d = 42;
double e = 3.14159;
int f = 0.0;Write your predictions before revealing ours.
Reveal answer
int a = 100; // Valid — 100 is an integer
int b = -50; // Valid — negative integers are fine
int c = 7.0; // Invalid — 7.0 is a double literal
double d = 42; // Valid — 42 becomes 42.0
double e = 3.14159; // Valid — this is a double value
int f = 0.0; // Invalid — 0.0 is a double literalNote: 7.0 and 0.0 look like whole numbers, but the decimal point makes them double literals in C#.
If any predictions were wrong, note what you missed.
Evaluating and Rebinding
The patterns from Chapter 1 apply unchanged. We can evaluate numeric variables and rebind them:
int x = 5;
int y = x;
x = 10;
int z = x + y;Line 4 uses addition. We cover arithmetic operators fully later in this chapter, but you already know what + does. Let us trace the state:
| after line | x | y | z |
|---|---|---|---|
| 1 | 5 | — | — |
| 2 | 5 | 5 | — |
| 3 | 10 | 5 | — |
| 4 | 10 | 5 | 15 |
Line 2 evaluates x (gets 5) and binds that value to y. Line 3 rebinds x to 10, but y is unaffected. Line 4 evaluates x (gets 10) and y (gets 5), computes their sum, and binds 15 to z.
This is value type behavior. Integers and doubles, like booleans, are value types. Each variable holds its own independent copy of the data. When we evaluated x in line 2, we copied the value 5 into y. The two variables are independent bindings.
Try it yourself.
Complete the state table:
double a = 1.5;
double b = 2.5;
double c = a + b;
a = 10.0;| after line | a | b | c |
|---|---|---|---|
| 1 | — | — | |
| 2 | — | ||
| 3 | |||
| 4 |
Reveal answer
| after line | a | b | c |
|---|---|---|---|
| 1 | 1.5 | — | — |
| 2 | 1.5 | 2.5 | — |
| 3 | 1.5 | 2.5 | 4.0 |
| 4 | 10.0 | 2.5 | 4.0 |
Line 4 rebinds a, but c already holds 4.0 from line 3. The binding is independent.
If your table differed, trace through each line again.
Observing Values
Console.WriteLine works with numbers just as it did with booleans:
int count = 42;
Console.WriteLine(count);Output: 42
The type signature: WriteLine: int → void
The translation: “Evaluate count and display the result to the console.”
Console.WriteLine also accepts doubles. It works the same way regardless of the numeric type.
Review
Before continuing, test yourself. Attempt each exercise from memory, then search this section to check your answers.
Definitions:
- What is an integer?
- What is a double?
- What are the bounds of an integer, and why?
Translations:
Translate each line to English:
int age = 25;double balance = 150.75;int next = current + 1;
Writing Code:
Write C# code for each description:
- Create an integer variable named items and bind the value 12 to it.
- Create a double variable named average and bind the value 85.5 to it.
- Create an integer variable named total and bind the result of evaluating count plus bonus to it.
State Table:
Complete the state table:
int p = 10;
int q = p;
p = 20;
int r = p + q;| after line | p | q | r |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 |
Check your answers against the examples in this section. Note what you missed and write corrected versions.
You now know how to store integers and doubles, choose between them, and track numeric state.
Previous: Chapter 2 Overview
Next: Section 2 - Computation