Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The return statement

Reference for C# return: ending a method, returning values, void and Task methods, ref returns, returning tuples and the rules about reachable code.

return ends the execution of the current method, constructor, property accessor, lambda or local function and hands control back to the caller. In a method with a non-void return type it must supply a value of the declared type (or one that converts to it). In a void method, constructor or set accessor it appears alone.

Every code path through a non-void method must end with a return or a throw; the compiler checks this at compile time. Use early return statements as guard clauses to keep the main logic flat.

C#
static int Clamp(int value, int min, int max)
{
    if (value < min) return min;
    if (value > max) return max;
    return value;
}

Try it

C# Early return and tuples

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# ref returns and finally

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
return;                  // void methods, constructors, set/init accessors
return expression;       // value-returning members
return ref expression;   // ref return (the method declares "ref T")
Name Description
expression A value implicitly convertible to the return type. In an async method returning Task<T>, you return a T, not the task.

Errors: returning a value from a void method fails with CS0127; a bare return; in a method that returns a value fails with CS0126; "not all code paths return a value" is CS0161. Returning a reference to a local variable is rejected (CS8168).

Usage notes

  • finally still runs. A return inside try evaluates its value first, then runs the finally block, then leaves. You cannot return from inside a finally block (CS0157).
  • Return types: in an async Task or async ValueTask method you write a bare return;; in async Task<T> you return the value. In an iterator, return is not allowed: use yield return / yield break.
  • Expression-bodied members (int Twice(int x) => x * 2;) return the expression without the keyword.
  • Several values: return a tuple, a record or use out parameters (see ref, out and in).
  • ref returns give the caller a reference to existing storage, so assignments through it modify the original. The target must outlive the method: fields, array elements and ref parameters are allowed, locals are not.
  • Unreachable code after a return produces warning CS0162.
  • Lambdas: return inside a lambda returns from the lambda only, not from the enclosing method.
  • Style: many early returns are fine for guards, but a long method with returns scattered everywhere is usually a sign it should be split.