Learning Path

Master programming fundamentals level by level, week by week

Select a level

The Combine

Master the fundamentals of C# programming - variables, control flow, data structures, and object-oriented basics

4 Weeks
Select a week

C# Basics

Variables, strings, conditionals, and console I/O

Topics

Variable Types in C#

Understanding different data types and when to use them

Key Points

  • C# is strongly typed - every variable must have a declared type
  • Value types (int, double, bool, char) store data directly
  • Reference types (string, arrays, objects) store references to data
  • Use var for type inference when the type is obvious
  • Choose the right type based on the data you need to store

Interactive Code Examples

// Integer types
int age = 25;                    // Whole numbers (-2,147,483,648 to 2,147,483,647)
long population = 8000000000L;   // Large whole numbers
short temperature = -15;         // Small whole numbers (-32,768 to 32,767)
byte level = 100;                // Very small (0 to 255)

// Decimal types
double price = 19.99;            // Floating point (15-16 digits precision)
decimal money = 1234.56m;        // High precision for money (28-29 digits)
float ratio = 0.75f;             // Less precise floating point

// Other value types
bool isActive = true;            // true or false
char grade = 'A';                // Single character
DateTime today = DateTime.Now;   // Date and time

💡 Explanation: Value types store actual data. Use int for whole numbers, double for decimals, bool for true/false, and char for single characters. The m suffix indicates decimal, f indicates float, and L indicates long.

Choosing the Right Type

int

Whole numbers without decimals

When to use:

Counting, indexing, IDs, ages, quantities

Example:

int age = 25; int count = 100;

double

Numbers with decimals (floating point)

When to use:

Measurements, calculations, scientific data

Example:

double temperature = 98.6; double distance = 5.5;

decimal

High precision decimals

When to use:

Money, financial calculations

Example:

decimal price = 19.99m; decimal balance = 1000.50m;

string

Text and characters

When to use:

Names, messages, user input, any text

Example:

string name = "Alice"; string message = "Hello";