Primitive Data Types
C# provides several built-in data types for storing different kinds of values. In this lesson, you'll learn about the most commonly used primitive types.
Integer Types
Integers are whole numbers (no decimal points).
int (Most Common)
csharp1int age = 25;2int score = -100;3int population = 1000000;
- Range: -2,147,483,648 to 2,147,483,647
- Size: 4 bytes (32 bits)
Other Integer Types
csharp1// Smaller integers2byte small = 255; // 0 to 255 (1 byte)3short medium = 32000; // -32,768 to 32,767 (2 bytes)45// Larger integers6long big = 9223372036854775807L; // Very large numbers (8 bytes)
| Type | Size | Range |
|---|---|---|
| byte | 1 byte | 0 to 255 |
| short | 2 bytes | -32,768 to 32,767 |
| int | 4 bytes | ±2.1 billion |
| long | 8 bytes | ±9.2 quintillion |
Use int for most cases. Use long only for very large numbers.
Floating-Point Types
For numbers with decimal points.
double (Most Common)
csharp1double price = 19.99;2double temperature = -40.5;3double pi = 3.14159265359;
- Precision: ~15-16 digits
- Size: 8 bytes
float
csharp1float height = 1.75f; // Note the 'f' suffix2float weight = 70.5f;
- Precision: ~6-7 digits
- Size: 4 bytes
- Requires
fsuffix
decimal (For Financial Calculations)
csharp1decimal salary = 50000.00m; // Note the 'm' suffix2decimal accountBalance = 1234.56m;3decimal taxRate = 0.0825m;
- Precision: 28-29 digits
- Size: 16 bytes
- Requires
msuffix - Best for money calculations (no rounding errors)
When to Use Each
csharp1// Use double for scientific calculations2double distance = 384400.0; // km to the moon34// Use decimal for money5decimal price = 29.99m;6decimal tax = price * 0.08m;7decimal total = price + tax;89// Use float when memory is critical and precision isn't10float gameCoordinate = 100.5f;
Boolean Type
For true/false values.
csharp1bool isActive = true;2bool hasPermission = false;3bool isGameOver = false;45// Boolean expressions6bool isAdult = age >= 18;7bool canVote = isAdult && isCitizen;
Booleans are essential for conditions and control flow (covered later).
Character Type
For single characters.
csharp1char letter = 'A';2char digit = '7';3char symbol = '@';4char newline = '\n';
Note: Use single quotes for char, double quotes for string.
Escape Characters
csharp1char tab = '\t'; // Tab2char newLine = '\n'; // New line3char quote = '\''; // Single quote4char backslash = '\\'; // Backslash
Comparing Types
csharp1// Integer vs floating-point2int wholeNumber = 10;3double decimalNumber = 10.5;45// Precision demonstration6double d = 0.1 + 0.2; // 0.30000000000000004 (not exact!)7decimal m = 0.1m + 0.2m; // 0.3 (exact)89// Boolean operations10bool a = true;11bool b = false;12bool result = a && b; // false (AND operation)
Type Information
You can check a variable's type:
csharp1int number = 42;2double price = 9.99;3string name = "Alice";45Console.WriteLine(number.GetType()); // System.Int326Console.WriteLine(price.GetType()); // System.Double7Console.WriteLine(name.GetType()); // System.String
Numeric Literals
C# supports different formats for numbers:
csharp1// Standard2int decimal = 255;34// Hexadecimal (prefix 0x)5int hex = 0xFF; // 25567// Binary (prefix 0b)8int binary = 0b11111111; // 255910// Digit separators (for readability)11int million = 1_000_000;12long billion = 1_000_000_000L;13double precise = 3.141_592_653_589;
Summary
In this lesson, you learned about C# primitive types:
| Category | Type | Use Case |
|---|---|---|
| Integers | int | Whole numbers (default choice) |
long | Very large whole numbers | |
| Floating-point | double | Decimal numbers (default choice) |
decimal | Financial/money calculations | |
| Boolean | bool | True/false values |
| Character | char | Single characters |
Choose int for whole numbers, double for decimals, decimal for money, and bool for conditions. Next, we'll explore strings in detail.