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.
static int Clamp(int value, int min, int max) { if (value < min) return min; if (value > max) return max; return value; }
Try it
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.
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
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
finallystill runs. Areturninsidetryevaluates its value first, then runs thefinallyblock, then leaves. You cannotreturnfrom inside afinallyblock (CS0157).- Return types: in an
async Taskorasync ValueTaskmethod you write a barereturn;; inasync Task<T>you return the value. In an iterator,returnis 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
outparameters (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
returnproduces warning CS0162. - Lambdas:
returninside 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.
Related
- yield, throw, ref, out and in, break and continue
- Lessons: Methods, Jump statements, Tuples