Thuta Learning
C#
BasicProgrammingBeginner

Variables

C#Lesson 520 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Variables
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Variables, in plain language

A variable is basically a little box that holds data. In C#, you have to specify the data type before declaring a variable. That way, the compiler can catch data type mistakes while you're still writing the code.

Problem → Why → Solution

The problem — why this concept matters

Failing to distinguish an uninitialized or default value from a valid domain value creates zero, false, and null bugs.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

A variable has a name, compile-time type, scope, lifetime, and current value. `var` is compile-time inference rather than dynamic typing; use `const`, nullable annotations, and the smallest useful scope deliberately.

Mental model
  1. typed input
  2. C# expression and method
  3. CLR value or object state

Explain the core concept and runtime behavior of Variables

Core syntax or pattern

csharp
string name = "Aung";
int age = 30;
double height = 5.8;
bool isStudent = false;

Console.WriteLine("Name: " + name);
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Height: {height}");
Console.WriteLine($"Student: {isStudent}");

A variable has a name, compile-time type, scope, lifetime, and current value. `var` is compile-time inference rather than dynamic typing; use `const`, nullable annotations, and the smallest useful scope deliberately.

Read the code step by step

  1. typed inputstring name = "Aung";

    A variable is basically a little box that holds data. In C#, you have to specify the data type before declaring a variable. That way, the compiler can catch data type mistakes while you're still writing the code.

  2. C# expression and methodConsole.WriteLine("Name: " + name);

    A variable has a name, compile-time type, scope, lifetime, and current value. `var` is compile-time inference rather than dynamic typing; use `const`, nullable annotations, and the smallest useful scope deliberately.

  3. CLR value or object stateConsole.WriteLine($"Student: {isStudent}");

    Read an age as text, validate it with `int.TryParse`, and test both success and failure branches.

Execution flow
  1. typed input
  2. C# expression and method
  3. CLR value or object state

Success

Explain the core concept and runtime behavior of Variables

Failure

Failing to distinguish an uninitialized or default value from a valid domain value creates zero, false, and null bugs.

A variable is basically a little box that holds data. In C#, you have to specify the data type before declaring a variable. That way, the compiler can catch data type mistakes while you're still writing the code.

csharp
string name = "Aung";
int age = 30;
double height = 5.8;
bool isStudent = false;

Console.WriteLine("Name: " + name);
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Height: {height}");
Console.WriteLine($"Student: {isStudent}");

What this code does

  • string holds text.
  • int holds whole numbers.
  • double holds decimal numbers.
  • bool holds true or false.
  • $"Age: {age}" is string interpolation — a cleaner way to drop a variable's value into a string.
You should see
Name: Aung Age: 30 Height: 5.8 Student: False

Info

⚠️ Common mistake

int age = "30"; will throw an error. "30" is text, while 30 is a number. Whether or not you add quotes changes the data type entirely.

Real-world usage

How to apply it in a real project

Read an age as text, validate it with `int.TryParse`, and test both success and failure branches.

Mini challenge

Build it yourself

Read an age as text, validate it with `int.TryParse`, and test both success and failure branches.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
string name = "Aung";
int age = 30;
double height = 5.8;
bool isStudent = false;

Console.WriteLine("Name: " + name);
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Height: {height}");
Console.WriteLine($"Student: {isStudent}");

Expected: Explain the core concept and runtime behavior of Variables

Show hint

Read an age as text, validate it with `int.TryParse`, and test both success and failure branches.

Quick quiz 1 / 3

What best describes the core idea of Variables?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • typed input → C# expression and method → CLR value or object state
  • A variable has a name, compile-time type, scope, lifetime, and current value. `var` is compile-time inference rather than dynamic typing; use `const`, nullable annotations, and the smallest useful scope deliberately.
  • Failing to distinguish an uninitialized or default value from a valid domain value creates zero, false, and null bugs.
  • Read an age as text, validate it with `int.TryParse`, and test both success and failure branches.

C# documentation — Microsoft Learn

Easy traps

  • Failing to distinguish an uninitialized or default value from a valid domain value creates zero, false, and null bugs.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Read an age as text, validate it with `int.TryParse`, and test both success and failure branches.

You'll know it worked when: Name: Aung Age: 30 Height: 5.8 Student: False

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Variables | Thuta Learning