DEV Community

Sabin Sim
Sabin Sim

Posted on

09. C# (Non-void Methods)

0. The Real Goal of This Lesson

This lesson is not about syntax.

It’s about learning how to lock a repeated line of reasoning into a single unit of thought,
and how to send the result of that thought back to the outside world.

In C#, that concept is called a non-void method.


1. Why Do We Need Return Values?

Let’s start from a human problem, not a language feature.

A problematic example

int a = 5;
int b = 10;

Console.WriteLine(a + b);
Enter fullscreen mode Exit fullscreen mode

This code runs correctly, but it has structural problems:

  • The result cannot be reused
  • The meaning of the calculation is not visible
  • Repeating the same calculation elsewhere leads to copy-paste

In other words:

This calculation represents a single decision or idea,
but in the code it simply disappears after execution.


2. How Humans Actually Think About This

A human doesn’t think:

“Print five plus ten.”

A human thinks:

“I need the result of adding two numbers.”

That implies:

  • Input: two numbers
  • Output: one meaningful result

This is exactly what a non-void method represents.


3. The Simplest Non-void Method

static int Add(int a, int b)
{
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Read this in plain English:

  • Add The name of a thought
  • int a, int b The information required to think
  • int (before the name) The type of result this thought produces
  • return The conclusion of the thought, sent outward

Key idea:

return is not a keyword — it is the conclusion of a decision.


4. Why return Is Strictly Enforced

An invalid method

static int Add(int a, int b)
{
    if (a > 10)
    {
        return a + b;
    }
}
Enter fullscreen mode Exit fullscreen mode

Compiler error:

Not all code paths return a value
Enter fullscreen mode Exit fullscreen mode

What the compiler is really saying:

“You promised to always give a result,
but some paths end without an answer.”

Rule:

Every possible execution path in a non-void method must return a value.


5. Method Definition vs Method Execution

This distinction is critical.

Definition (design)

static int Add(int a, int b)
{
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

At this point:

  • Nothing runs
  • No calculation happens
  • This is only a registered blueprint

Execution (call)

var result = Add(5, 10);
Enter fullscreen mode Exit fullscreen mode

Now the real flow happens:

  1. Control enters Add
  2. a = 5, b = 10
  3. return a + b15
  4. The value is assigned to result

6. Why F10 and F11 Feel So Different in Debugging

This difference reflects how you observe thought flow.

Step Over (F10)

“I trust this method. Just show me the result.”

  • Skips the internal logic
  • Jumps directly to the returned value

Step Into (F11)

“I want to see how this thought unfolds.”

  • Enters the method
  • Shows parameter values
  • Stops at the return

For beginners, Step Into is essential.


7. Methods That Return bool Represent Decisions

Requirement in human language:

“This text should not be too long.”

That’s not output.
That’s a judgment.

static bool IsLong(string text)
{
    return text.Length > 10;
}
Enter fullscreen mode Exit fullscreen mode

Why this is good:

  • No if/else noise
  • The condition itself is the return value
  • The intent is obvious when read

8. Why else Became Unnecessary

Initial version:

if (text.Length > 10)
{
    return true;
}
else
{
    return false;
}
Enter fullscreen mode Exit fullscreen mode

Key rule:

When return executes, the method immediately ends.

So this is equivalent:

if (text.Length > 10)
{
    return true;
}

return false;
Enter fullscreen mode Exit fullscreen mode

This is not a trick.
This is simplifying the thought structure.

That process is called refactoring.


9. What Refactoring Actually Means

Refactoring is changing how the code reads,
without changing what the code does.

  • Behavior stays the same
  • Structure becomes clearer
  • Maintenance becomes easier
  • Errors become less likely

10. IDE Assistance Is Structural, Not Cognitive

var isLong = IsLong(userInput);
Enter fullscreen mode Exit fullscreen mode

If IsLong doesn’t exist yet:

  • Compiler error appears
  • Quick action → “Generate method”

What the IDE does:

  • Creates the skeleton
  • Enforces syntax

What it does not do:

  • Decide the logic
  • Think for you

The thinking is still yours.


11. Why Method Order Does Not Matter

Variables:

  • Must be declared before use

Methods:

  • Are registered as blueprints for the entire file

This allows you to:

Write what you want to do first,
and explain how it works later.

That matches how humans think.


Final One-Line Summary

A non-void method is a named thought that produces a conclusion,
and return is the moment that thought ends and hands back its result.

Top comments (0)