Proposal: return? — Soft or Conditional Return Statement #9773
Replies: 6 comments 21 replies
-
|
This was proposed only last week, and was rejected then, too. |
Beta Was this translation helpful? Give feedback.
-
|
Do you have that issue link? |
Beta Was this translation helpful? Give feedback.
-
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.
-
|
Hi everyone, I want to share a real-world use case that demonstrates the value of Current RealityIn codebases using the Result pattern for error handling, this appears thousands of times: Issues:
With
|
Beta Was this translation helpful? Give feedback.
-
|
Hi, I do have a question about the type that is returned from the code snippet. What does that snippet return actually? I think, the root cause of the boilerplate is usage of error objects to indicate failure - instead of using exceptions - which where designed those days to avoid such code. Usage of exceptions allows for non-local error handling (additional to local handling) - contrary to return codes - which must be handled all over the place immediately after the invocation took place. I think C# already has a nice and clean way to deal with such cases. And for those who want otherwise, they could do otherwise... |
Beta Was this translation helpful? Give feedback.
-
|
I like null conditional returns, but I'd prefer this syntax.
I don't think we should have to use an Exception for an early exit, and there are other syntactical sugar things in the language to reduce the number of null checks that you need to do. |
Beta Was this translation helpful? Give feedback.
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