Variables — Execution-Focused Notes (C# Fundamentals)
0. Goal of This Lesson (Just One Thing)
A variable exists to store a value
so it can be reused later in the program flow.
Understanding by definition ❌
Understanding by running the code ✅
1. The Minimum Program Structure (Required)
📌 Without this structure, nothing runs in C#.
using System;
class Program
{
static void Main()
{
// All example code goes here
}
}
- C# programs start from
Main() - Writing a single line of code outside this structure does nothing
2. The Most Basic Variable (string)
Code
using System;
class Program
{
static void Main()
{
string userInput = "A";
Console.WriteLine(userInput);
}
}
Output
A
What to notice
- The value
"A"is stored - It is stored in a variable named
userInput - The stored value is later retrieved and printed
👉 This is the entire reason variables exist.
3. Values Can Change, Types Cannot
Changing the value
using System;
class Program
{
static void Main()
{
string userInput = "A";
Console.WriteLine(userInput);
userInput = "ABC";
Console.WriteLine(userInput);
}
}
Output
A
ABC
What happens if you add this?
userInput = 1;
❌ Compile-time error
Why?
-
userInputis a string variable - It can only store text, not numbers
4. Integer Variables (int)
Code
using System;
class Program
{
static void Main()
{
int number = 4;
Console.WriteLine(number);
number = 10;
Console.WriteLine(number);
}
}
Output
4
10
📌 Rule:
-
intvariables store only integers - Strings are not allowed
5. Declaration and Initialization Are Not the Same
❌ Invalid example (try it)
using System;
class Program
{
static void Main()
{
int number;
Console.WriteLine(number);
}
}
Compile-time error:
Use of unassigned local variable 'number'
Plain explanation
- The variable exists
- But no value was placed inside
- You cannot use an empty variable
✅ Correct order
using System;
class Program
{
static void Main()
{
int number;
number = 4;
Console.WriteLine(number);
}
}
Output
4
6. Using a Variable Before Declaration Always Fails
❌ Example
using System;
class Program
{
static void Main()
{
Console.WriteLine(number);
int number = 4;
}
}
Error:
Cannot use local variable 'number' before it is declared
📌 Rule summary
- Declare
- Assign
- Use
Breaking this order always fails.
7. Why Variables Matter (Simple App Context)
Example
using System;
class Program
{
static void Main()
{
Console.WriteLine("S - Show items");
Console.WriteLine("A - Add item");
Console.WriteLine("R - Remove item");
Console.WriteLine("E - Exit");
string userInput = Console.ReadLine();
Console.WriteLine("You selected: " + userInput);
}
}
Execution flow
- Menu is displayed
- Program waits for user input
- Input is stored in a variable
- Stored value is reused
👉 A variable is a memory slot.
8. What NOT to Memorize
❌ Formal definitions
❌ Error message wording
❌ Terminology lists
✅ Keep only this mental model:
Create a box → put a value in → use it later
9. Self-Check (If You Can Do These, You Understand)
Try this on your own:
- Replace
"A"with"HELLO" - Add one more
Console.WriteLine - Write
userInput = 3;and explain why it fails
If you can explain the failure clearly,
you understand variables at a fundamental level.
Closing Thought
This post is not about finishing a project.
It’s about rebuilding confidence in the basics,
by executing, observing, and reasoning — not memorizing.
More fundamentals coming next.
Top comments (0)