Lesson 1: Introduction to Variables in C#
Variables are the building blocks of every C# program. Before you can work with data — numbers, text, dates, or anything else — you need to know how to store it. In this lesson, we'll cover exactly what variables are, how to declare and initialize them, and the rules you must follow when naming them.
What is a Variable?
A variable is a named storage location in memory that holds a value. Think of it like a labeled box — the label is the variable name, and whatever you put inside is the value.
In C#, every variable has a type (what kind of data it stores) and a name (how you refer to it in code).
Declaring a Variable
To declare a variable in C#, you specify the type followed by the name:
// Syntax: type variableName;
int age;
string name;
double salary;
bool isActive;
At this point the variables are declared but not yet assigned a value. Trying to use them before assigning a value will cause a compile-time error in C#.
Initializing a Variable
You can declare and assign a value at the same time — this is called initialization:
// Declare and initialize in one line
int age = 25;
string name = "Alice";
double salary = 72500.50;
bool isActive = true;
// Or declare first, then assign later
int score;
score = 100;
Console.WriteLine(age); // Output: 25
Console.WriteLine(name); // Output: Alice
Console.WriteLine(isActive); // Output: True
The var Keyword
C# supports type inference using the var keyword. The compiler automatically
determines the type based on the assigned value. The type is still fixed at compile time — var
is not the same as JavaScript's dynamic typing.
var age = 25; // inferred as int
var name = "Alice"; // inferred as string
var pi = 3.14; // inferred as double
var flag = true; // inferred as bool
// This would cause a compile error — type is locked:
// age = "hello"; ❌ Cannot convert string to int
💡 Best Practice: Use explicit types (int, string) when the type isn't obvious from the value. Use var when the type is clear and makes the code less verbose.
Variable Naming Rules
C# enforces strict naming rules. Your variable names must follow these:
| Rule | Valid Example | Invalid Example |
|---|---|---|
Must start with a letter or underscore _ | age, _count | 1age, @name |
| Can contain letters, digits, underscores | firstName, total_2 | first-name, total! |
| Cannot be a C# reserved keyword | myClass, isNew | class, int, new |
| Case-sensitive | Age ≠ age | — |
Naming Conventions
Following conventions makes your code easier for others (and future you) to read:
camelCase for local variables and parameters: firstName, totalPrice, isLoggedIn
PascalCase for class names and properties: CustomerName, TotalAmount
_camelCase for private fields: _connectionString, _userId
Use descriptive names: userAge is far better than x or a1
Constants: Variables That Don't Change
If a value should never change after it's set, use the const keyword. Constants are
evaluated at compile time and cannot be modified at runtime.
const double Pi = 3.14159265358979;
const int MaxRetries = 3;
const string AppName = "FormatBase";
// This would cause a compile error:
// Pi = 3.14; ❌ Cannot assign to a constant
A Complete Example
Let's put it all together in a simple console program:
// A simple student profile
using System;
const string SchoolName = "FormatBase Academy";
string studentName = "Ravi Patel";
int studentAge = 21;
double gpa = 3.85;
bool isEnrolled = true;
Console.WriteLine($"School: {SchoolName}");
Console.WriteLine($"Student: {studentName}, Age: {studentAge}");
Console.WriteLine($"GPA: {gpa}, Enrolled: {isEnrolled}");
/* Output:
School: FormatBase Academy
Student: Ravi Patel, Age: 21
GPA: 3.85, Enrolled: True
*/
🧠 Quick Check — Lesson 1
Which of the following is a valid C# variable name?
Lesson Summary
A variable is a named memory location that stores a value of a specific type.
You declare variables with type name; and initialize with type name = value;
The var keyword lets the compiler infer the type — but it's still strongly typed.
Variable names must start with a letter or underscore, and cannot be C# keywords.
Use const for values that should never change at runtime.
Follow naming conventions: camelCase for locals, PascalCase for classes/properties.