Proposal: return? — Soft or Conditional Return Statement #9773
Unanswered
hosseinjafari
asked this question in
Language Ideas
Replies: 3 comments 17 replies
-
|
This was proposed only last week, and was rejected then, too. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Do you have that issue link? |
Beta Was this translation helpful? Give feedback.
4 replies
-
public string HandleRequest(Request req)
{
var auth = CheckAuth(req);
if (auth != null)
return auth;
var validation = Validate(req);
if (validation != null)
return validation;
return "Success";
}Can already be simplified as: => CheckAuth() ?? Validate() ?? "Success"; |
Beta Was this translation helpful? Give feedback.
13 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
A proposal to introduce a new lightweight early-return syntax,
return?, that allows methods to optionally return early when a non-null result is produced — eliminating redundantif (x != null) return x;checks.Full detailed proposal attached below.
Proposal:
return?— Soft or Conditional Return StatementAuthor: Hoosein Jafari (By ChatGPT)
Status: Proposed
Language Version: C# (future)
Discussion: [TBD — GitHub Issue URL once created]
Summary
Introduce a new statement form
return? <expression>;that allows a method to optionally return early only if the given expression produces a non-null result (or otherwise meaningful value).This mechanism enables more expressive, cleaner, and safer early-exit flows without explicit null-checks in the caller method.
Motivation
In modern C#, conditional returns are common patterns, especially in validation, authorization, or multi-step methods where each stage may decide to return early.
Example (current C#):
This pattern is verbose and repetitive, obscuring the main control flow.
The developer’s intent (“return if something is produced”) is diluted by boilerplate code.
Detailed Design
Syntax
A new statement form is introduced:
It means:
The expression must be type-compatible with the method’s return type.
Semantics
The compiler would conceptually translate:
to:
This is purely syntactic sugar and introduces no new runtime behavior.
Nullable and Generic Return Types
If the method’s return type is nullable (
T?) or an optional-like generic (Maybe<T>,Option<T>),return?naturally extends to these:If neither method yields a value, the method simply continues execution.
Examples
Example 1: Validation Chain
Each call either produces a response (early return) or allows execution to continue seamlessly.
Example 2: Simplifying Nested Logic
Control Flow Safety
return?does not bypassfinallyblocks.return.Optional Extension:
continue;in Method ScopeAn optional conceptual sibling,
continue;, could act as “skip current path and return control to caller”, but this remains outside the scope of this proposal.Future discussion could explore
continue;oryield?variants for async and iterator contexts.Benefits
if (x != null) return x;patterns?.or??=Drawbacks
Alternatives Considered
if-returnexpressionMaybe<T>)— powerful but requires extra type wrapping.
return?)— simplest, expressive, and clear.
Design Notes
return?as potential early exit.Relationship to Existing Features
?.)??=)is not nullchecksOpen Questions
default(T)as “empty” for value types?async Task<T>methods?(Likely identical translation with
awaitsemantics preserved.)Conclusion
The
return?statement offers a simple yet elegant improvement to everyday C# code patterns, reducing noise and improving intent clarity.By introducing a soft return, developers can write cleaner, more expressive code without changing the underlying semantics of method control flow.
Authored by Hoosein Jafari
Date: October 2025
CSharp_ReturnQuestion_Proposal.md
Beta Was this translation helpful? Give feedback.
All reactions