DEV Community

Sabin Sim
Sabin Sim

Posted on

08. C# (Methods)

0) Goal of This Lesson

Understanding why we stop repeating code
and start calling behavior by name

If you miss this idea, you will:

  • copy and paste the same logic
  • fix the same bug in multiple places
  • lose control as the codebase grows

A method is not a shortcut.
It is a structure for controlling change.


1) The Problem We Are Actually Facing

Consider a simple TODO menu.

User input:

  • S → See all TODOs
  • A → Add TODO
  • R → Remove TODO
  • E → Exit

And we print messages like this:

Selected option: See all TODOs
Selected option: Add TODO
Selected option: Remove TODO
Selected option: Exit
Enter fullscreen mode Exit fullscreen mode

A Naive Implementation

if (choice == "S")
{
    Console.WriteLine("Selected option: See all TODOs");
}
else if (choice == "A")
{
    Console.WriteLine("Selected option: Add TODO");
}
else if (choice == "R")
{
    Console.WriteLine("Selected option: Remove TODO");
}
else if (choice == "E")
{
    Console.WriteLine("Selected option: Exit");
}
Enter fullscreen mode Exit fullscreen mode

This code works.
But it hides a future problem.


2) Why This Becomes a Problem Later

Right now, nothing is broken.

But change is guaranteed.

Imagine this request:

“Change Selected option to User’s choice everywhere.”

What happens?

  • multiple places to update
  • easy to miss one
  • no single source of truth

This is how bugs are introduced structurally, not logically.


3) The Core Idea: Name the Behavior

At this point, we need a shift in thinking.

This block of code represents one action.
If it is one action, it deserves one name.

That name is a method.


4) What a Method Really Is (Plain English)

A method is a definition of behavior
that runs only when explicitly called.

Important clarification:

  • defining a method does nothing
  • calling a method triggers execution

Definition and execution are separate concepts.


5) Defining a Void Method

static void PrintSelectedOption(string option)
{
    Console.WriteLine("Selected option: " + option);
}
Enter fullscreen mode Exit fullscreen mode

6) Breaking Down the Definition

static void PrintSelectedOption(string option)
Enter fullscreen mode Exit fullscreen mode

Observation:

  • PrintSelectedOption describes an action
  • method names use verbs
  • PascalCase is the C# convention

Rule:

Method names describe behavior, not data.


The void Keyword

Observation:

  • this method does not return a value
  • it only performs an action (printing)

Rule:

Use void when a method produces side effects, not results.


The Parameter

(string option)
Enter fullscreen mode Exit fullscreen mode

Observation:

  • this is a placeholder
  • it has no value yet

Rule:

Parameters receive values only at call time.


7) Definition vs Call (Critical Distinction)

At this point:

Method defined
Nothing executed
Enter fullscreen mode Exit fullscreen mode

No output appears.


Calling the Method

PrintSelectedOption("See all TODOs");
Enter fullscreen mode Exit fullscreen mode

Observation:

  • the method body runs
  • option receives the value "See all TODOs"
  • output is produced

Rule:

Methods execute only when called.


8) Refactoring the Original Code

if (choice == "S")
{
    PrintSelectedOption("See all TODOs");
}
else if (choice == "A")
{
    PrintSelectedOption("Add TODO");
}
else if (choice == "R")
{
    PrintSelectedOption("Remove TODO");
}
else if (choice == "E")
{
    PrintSelectedOption("Exit");
}
Enter fullscreen mode Exit fullscreen mode

Observation:

  • duplication removed
  • behavior centralized
  • intent clearer

Rule:

Common behavior should exist in exactly one place.


9) Parameter vs Argument (Clear Distinction)

Definition side:

static void PrintSelectedOption(string option)
Enter fullscreen mode Exit fullscreen mode
  • option → parameter

Call side:

PrintSelectedOption("Add TODO");
Enter fullscreen mode Exit fullscreen mode
  • "Add TODO" → argument

Rule:

Parameters receive arguments at the moment of invocation.


10) What We Actually Gained

Now, changing the message means editing one line:

Console.WriteLine("User's choice: " + option);
Enter fullscreen mode Exit fullscreen mode

Effects:

  • lower bug risk
  • easier testing
  • readable structure
  • controlled change

This is the foundation of clean code.


11) Recognizing Methods You Already Use

Console.WriteLine("Hello");
Enter fullscreen mode Exit fullscreen mode

Observation:

  • WriteLine is a method
  • it takes arguments
  • it returns nothing (void)
var input = Console.ReadLine();
Enter fullscreen mode Exit fullscreen mode

Observation:

  • ReadLine is also a method
  • it returns a value (string)

Rule:

Methods may return nothing or return data.

This leads directly to the next topic.


12) One-Line Summary

Methods are not about reducing code size.
They are about creating a structure that survives change.

Top comments (0)