Integer types
Learn int, long, short, byte and sbyte types and their ranges.
What are value types?
In C#, value types store their data directly. int holds whole numbers, double holds decimal numbers, char holds a single character, and bool holds true or false.
These types live on the stack; when you assign one, the value is copied. After int a = 5; and int b = a;, b is an independent copy.
Value types are small and fast; they take memory as soon as they are declared. Because they are copied on assignment, two variables stay independent.
char and bool
A char holds a single Unicode character in single quotes: char grade = 'A';. Do not use double quotes; 'A' is a char, while "A" is a string.
A bool takes only two values: true or false. It is used in conditions and flags. Example: bool isOpen = true;
Both are value types and live on the stack. A char can be converted to its numeric code; a bool is the backbone of logical comparisons.
Exercise Print four value types
The variables age, height, grade, and isAdult are declared below. Print each one on its own line with Console.WriteLine, writing only the value.
Show example solution
Print each variable with a separate Console.WriteLine.
Example solution:
using System;
class Program {
static void Main() {
int age = 30;
double height = 1.75;
char grade = 'A';
bool isAdult = true;
Console.WriteLine(age);
Console.WriteLine(height);
Console.WriteLine(grade);
Console.WriteLine(isAdult);
}
}
Test yourself
-
Which one is an int variable?
- double price = 9.99;
- int count = 42;
- string name = "Alex";
- var city = "London";
Show answer
int count = 42; — Correct! int holds a whole number with no decimal point; 42 is an int.
-
Assign the grade letter as a char
char grade = ____;Show answer
char grade = 'A'; -
Match each type to its literal
- int
- 42
- double
- 3.14
- char
- 'Z'
- bool
- true