1.5.17 Writing Methods That Return on Every Code Path

Last Updated: 9/21/2026
A Non-void Method Must Produce Its Promised Result

A method declared with a return type such as:

int

promises its caller an integer result.

For example:

public int GetGoalCategory()
{
    return 1;
}

The caller expects the method to produce an int.

When a method contains conditional paths, each possible path must still lead to a valid result.

A Conditional Can Create More Than One Code Path

Consider:

public string GetAvailabilityText()
{
    if (this.isAvailable)
    {
        return "Available";
    }
}

There is a problem.

When:

isAvailable = true

the method returns a string.

When:

isAvailable = false

execution can reach the end of the method without returning the promised string.

That leaves one code path without a result.

Use if/else So Both Paths Return

A complete version is:

public string GetAvailabilityText()
{
    if (this.isAvailable)
    {
        return "Available";
    }
    else
    {
        return "Unavailable";
    }
}

Now both possible paths produce a string.

Conceptually:

isAvailable?
  ├─ true  → return "Available"
  └─ false → return "Unavailable"

Every path sends a result to the caller.

Trace Every Path Separately

When reviewing a returning method with a decision, do not inspect only the branch you expect most often.

Trace:

  • true path;
  • false path.

For each path, ask:

An if/else if Chain Also Needs a Result for Every Possible Outcome

Consider:

public string GetGoalCategory(int goals)
{
    if (goals >= 3)
    {
        return "High scoring";
    }
    else if (goals == 2)
    {
        return "Two goals";
    }
    else
    {
        return "Fewer than two";
    }
}

The final else provides a result when neither earlier condition is true.

Every branch returns a string.

A Final Return Can Also Cover Remaining Paths

Another pattern is:

public string GetGoalCategory(int goals)
{
    if (goals >= 3)
    {
        return "High scoring";
    }

    return "Fewer than three";
}

If the first condition is true, the first return executes.

If it is false, execution continues to the final return.

Both paths produce a string.

Distinguish void from Returning Methods

A void method does not promise a data value:

public void DisplayScore()
{
    System.Console.WriteLine(this.score);
}

It can finish without returning a value.

A non-void method does promise a result:

public int GetScore()

Its valid execution paths must produce an integer result.

The return type changes what the method is required to provide.

Match Every Returned Value to the Declared Type

For:

public int GetScore()

this is compatible:

return this.score;

A string such as:

return "three";

does not match the declared integer result.

Completeness and type compatibility both matter.

Use a Path Table

For a simple method:

public string GetAvailabilityText()
{
    if (this.isAvailable)
    {
        return "Available";
    }
    else
    {
        return "Unavailable";
    }
}

you can reason with:

| Condition | Path | Returned value | | --- | --- | --- | | isAvailable == true | if | "Available" | | isAvailable == false | else | "Unavailable" |

Every possible condition result has a returned string.

Nested Decisions Create More Paths to Inspect

Suppose a returning method contains a nested decision.

Do not assume that one return somewhere inside the nested structure covers the entire method.

Trace each possible combination of reached branches.

The question remains:

Use the Compiler Message as Evidence

When C# reports that not all code paths return a value, inspect the method's conditional structure.

The message is telling you that at least one possible execution path can finish without providing the declared result.

Do not silence the message by adding an arbitrary return value.

Choose a return that matches the requirement.

Return Logic Should Match the Requirement

If a method is supposed to classify a score, each returned value should describe the correct category.

A method that returns something on every path can still be logically wrong.

You need both:

  • every path returns;
  • each path returns the correct result.
Key Review Process

For a non-void method:

  1. identify the declared return type;
  2. identify every possible decision path;
  3. trace each path to its end;
  4. confirm each path reaches a return;
  5. confirm each returned expression matches the type;
  6. confirm each returned value matches the requirement.

A returning method is complete only when every valid execution path produces the promised result.