Skip to main content

Day 2: Variables and Data Types

What is a Variable?

A variable is a named container that stores data in your program. Think of it like a labeled box where you can put information and retrieve it later. Every variable has a name, a type, and a value.

// This is a variable declaration
int age = 25;

In the example above:

  • int is the data type (it stores whole numbers)
  • age is the variable name
  • 25 is the value assigned to the variable

Common Data Types in C#

C# is a statically-typed language, which means you must declare what type of data a variable will hold. Here are the most common data types:

Numeric Types

Integer Types (Whole Numbers)

// int - most commonly used for whole numbers
int score = 100;
int temperature = -5;

// long - for very large whole numbers
long worldPopulation = 8000000000L;

// byte - small numbers from 0 to 255
byte age = 30;

// short - numbers from -32,768 to 32,767
short year = 2026;

Floating-Point Types (Decimal Numbers)

// double - most commonly used for decimals
double price = 19.99;
double pi = 3.14159265359;

// float - less precise, uses 'f' suffix
float height = 5.9f;

// decimal - highest precision, used for money
decimal accountBalance = 1234.56m;

When to use what?

  • Use int for most whole numbers
  • Use double for most decimal numbers
  • Use decimal for financial calculations (money)
  • Use float when storage space matters more than precision

Text Types

// char - a single character (uses single quotes)
char grade = 'A';
char initial = 'M';

// string - text/multiple characters (uses double quotes)
string name = "John Doe";
string message = "Hello, World!";

Boolean Type

// bool - true or false
bool isLoggedIn = true;
bool hasPermission = false;

Variable Naming Rules

  1. Must start with a letter or underscore (_)
  2. Can contain letters, numbers, and underscores
  3. Cannot use C# keywords (like int, class, if, etc.)
  4. Case-sensitive (name and Name are different variables)

Good Naming Practices

// ✅ Good - descriptive and follows camelCase convention
string firstName = "Jane";
int totalScore = 95;
bool isActive = true;

// ❌ Bad - unclear, uses Hungarian notation
string strN = "Jane";
int x = 95;
bool b1 = true;

Tip: Use camelCase for variable names (start with lowercase, capitalize each new word).

Declaring and Initializing Variables

Declaration Only

// Declare a variable without assigning a value
int numberOfStudents;

// Assign a value later
numberOfStudents = 30;

Declaration with Initialization

// Declare and assign in one line (recommended)
int numberOfStudents = 30;
string schoolName = "Tech Academy";

Multiple Variables

// Declare multiple variables of the same type
int x = 5, y = 10, z = 15;

// Or on separate lines (more readable)
int x = 5;
int y = 10;
int z = 15;

Type Inference with var

You can use the var keyword to let C# infer the type from the value:

var name = "Alice";        // C# knows this is a string
var age = 28; // C# knows this is an int
var price = 29.99; // C# knows this is a double
var isValid = true; // C# knows this is a bool

// Once declared, the type is fixed
// age = "thirty"; // ❌ Error! Can't change type

Note: var is just a shortcut for writing the type name. The variable still has a specific type.

Constants

If you have a value that should never change, use the const keyword:

const double Pi = 3.14159;
const int DaysInWeek = 7;
const string CompanyName = "Acme Corp";

// Pi = 3.14; // ❌ Error! Cannot modify a constant

Practical Example: Calculator

Let's put it all together with a simple calculator:

using System;

class Program
{
static void Main(string[] args)
{
// Declare variables
int number1 = 10;
int number2 = 5;

// Perform calculations
int sum = number1 + number2;
int difference = number1 - number2;
int product = number1 * number2;
double quotient = (double)number1 / number2; // Cast to double for decimal result

// Display results
Console.WriteLine($"Number 1: {number1}");
Console.WriteLine($"Number 2: {number2}");
Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Difference: {difference}");
Console.WriteLine($"Product: {product}");
Console.WriteLine($"Quotient: {quotient}");
}
}

Output:

Number 1: 10
Number 2: 5
Sum: 15
Difference: 5
Product: 50
Quotient: 2

Exercise: Your Turn!

Try creating a program that stores information about yourself:

using System;

class Program
{
static void Main(string[] args)
{
// Store your information
string yourName = "Your Name Here";
int yourAge = 0;
double yourHeight = 0.0;
bool isStudent = false;
char favoriteGrade = 'A';

// Display your information
Console.WriteLine($"Name: {yourName}");
Console.WriteLine($"Age: {yourAge}");
Console.WriteLine($"Height: {yourHeight} meters");
Console.WriteLine($"Student: {isStudent}");
Console.WriteLine($"Favorite Grade: {favoriteGrade}");
}
}

Challenge: Modify the program to calculate and display your age in months and days!

Key Takeaways

  • ✅ Variables store data with a name, type, and value
  • ✅ Common types: int, double, decimal, string, char, bool
  • ✅ Use descriptive variable names in camelCase
  • ✅ Use const for values that never change
  • ✅ Use var for cleaner code when the type is obvious

What's Next?

In the next lesson, we'll learn about operators and expressions - how to perform calculations, compare values, and combine conditions in C#!


Questions or struggling with something? Variables are fundamental to programming, so make sure you're comfortable with them before moving on. Try the exercise above and experiment with different data types!