DEV Community

Sabin Sim
Sabin Sim

Posted on

01. C# (Variables)

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
    }
}
Enter fullscreen mode Exit fullscreen mode
  • 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

A
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

A
ABC
Enter fullscreen mode Exit fullscreen mode

What happens if you add this?

userInput = 1;
Enter fullscreen mode Exit fullscreen mode

❌ Compile-time error

Why?

  • userInput is 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

4
10
Enter fullscreen mode Exit fullscreen mode

📌 Rule:

  • int variables 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Compile-time error:

Use of unassigned local variable 'number'
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

4
Enter fullscreen mode Exit fullscreen mode

6. Using a Variable Before Declaration Always Fails

❌ Example

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(number);
        int number = 4;
    }
}
Enter fullscreen mode Exit fullscreen mode

Error:

Cannot use local variable 'number' before it is declared
Enter fullscreen mode Exit fullscreen mode

📌 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Execution flow

  1. Menu is displayed
  2. Program waits for user input
  3. Input is stored in a variable
  4. 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)