12 minlesson

Primitive Data Types

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)

csharp
1int 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

csharp
1// Smaller integers
2byte small = 255; // 0 to 255 (1 byte)
3short medium = 32000; // -32,768 to 32,767 (2 bytes)
4
5// Larger integers
6long big = 9223372036854775807L; // Very large numbers (8 bytes)
TypeSizeRange
byte1 byte0 to 255
short2 bytes-32,768 to 32,767
int4 bytes±2.1 billion
long8 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)

csharp
1double price = 19.99;
2double temperature = -40.5;
3double pi = 3.14159265359;
  • Precision: ~15-16 digits
  • Size: 8 bytes

float

csharp
1float height = 1.75f; // Note the 'f' suffix
2float weight = 70.5f;
  • Precision: ~6-7 digits
  • Size: 4 bytes
  • Requires f suffix

decimal (For Financial Calculations)

csharp
1decimal salary = 50000.00m; // Note the 'm' suffix
2decimal accountBalance = 1234.56m;
3decimal taxRate = 0.0825m;
  • Precision: 28-29 digits
  • Size: 16 bytes
  • Requires m suffix
  • Best for money calculations (no rounding errors)

When to Use Each

csharp
1// Use double for scientific calculations
2double distance = 384400.0; // km to the moon
3
4// Use decimal for money
5decimal price = 29.99m;
6decimal tax = price * 0.08m;
7decimal total = price + tax;
8
9// Use float when memory is critical and precision isn't
10float gameCoordinate = 100.5f;

Boolean Type

For true/false values.

csharp
1bool isActive = true;
2bool hasPermission = false;
3bool isGameOver = false;
4
5// Boolean expressions
6bool isAdult = age >= 18;
7bool canVote = isAdult && isCitizen;

Booleans are essential for conditions and control flow (covered later).

Character Type

For single characters.

csharp
1char letter = 'A';
2char digit = '7';
3char symbol = '@';
4char newline = '\n';

Note: Use single quotes for char, double quotes for string.

Escape Characters

csharp
1char tab = '\t'; // Tab
2char newLine = '\n'; // New line
3char quote = '\''; // Single quote
4char backslash = '\\'; // Backslash

Comparing Types

csharp
1// Integer vs floating-point
2int wholeNumber = 10;
3double decimalNumber = 10.5;
4
5// Precision demonstration
6double d = 0.1 + 0.2; // 0.30000000000000004 (not exact!)
7decimal m = 0.1m + 0.2m; // 0.3 (exact)
8
9// Boolean operations
10bool a = true;
11bool b = false;
12bool result = a && b; // false (AND operation)

Type Information

You can check a variable's type:

csharp
1int number = 42;
2double price = 9.99;
3string name = "Alice";
4
5Console.WriteLine(number.GetType()); // System.Int32
6Console.WriteLine(price.GetType()); // System.Double
7Console.WriteLine(name.GetType()); // System.String

Numeric Literals

C# supports different formats for numbers:

csharp
1// Standard
2int decimal = 255;
3
4// Hexadecimal (prefix 0x)
5int hex = 0xFF; // 255
6
7// Binary (prefix 0b)
8int binary = 0b11111111; // 255
9
10// 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:

CategoryTypeUse Case
IntegersintWhole numbers (default choice)
longVery large whole numbers
Floating-pointdoubleDecimal numbers (default choice)
decimalFinancial/money calculations
BooleanboolTrue/false values
CharactercharSingle characters

Choose int for whole numbers, double for decimals, decimal for money, and bool for conditions. Next, we'll explore strings in detail.