Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: ??= operator #8894

Closed
manixrock opened this issue Feb 18, 2016 · 1 comment
Closed

Proposal: ??= operator #8894

manixrock opened this issue Feb 18, 2016 · 1 comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue

Comments

@manixrock
Copy link

Null-coalescing assignment operator, or fallback operator.

Allows this:

myNullableVariable = myNullableVariable ?? c;

To be re-written as this:

myNullableVariable ??= c;

It would be quite useful for lazy-initialized properties:

Person LoveMatch => mLoveMatchCache ?? mLoveMatchCache = Suitors.First(s => ComplexMatchingAlgorithm(s));
private Person mLoveMatchCache;

To become easier to write and understand:

Person LoveMatch => mLoveMatchCache ??= Suitors.First(s => ComplexMatchingAlgorithm(s));
private Person mLoveMatchCache;

It would also allow for variable fallbacks:

Person FindMatch(Person person, IEnumerable<Person> friends = null) {
    friends ??= FindFriends(person);
    // ...
}

It would work much like the other assignment operators. Of course, the right-hand side of the operation would only be executed if the left-hand side is null.

@HaloFour
Copy link

Dupe of (already closed as wont-fix) #205

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

4 participants