22

It would be really nice if C# allowed an ??= operator. I've found myself writing the following frequently:

something = something ?? new Something();

I'd rather write it like this:

something ??= new Something();

Thoughts? New language extensions are always controversial by their nature.

CodeMonkeyKing
  • 4,370
  • 1
  • 31
  • 34
  • I would avoid packing too many rather obscure constructs into the language, if I were C# chief architect..... Plus, the ??== operator really doesn't seem all that intuitive to me, quite honestly. – marc_s Mar 17 '09 at 19:57
  • 3
    duplicate http://stackoverflow.com/questions/558010/why-cant-i-do-in-c – Amy B Mar 17 '09 at 20:02
  • 4
    It wouldn't be new, it would follow the shorthand convention of other operators – Ed S. Mar 17 '09 at 20:02
  • Please see the duplicate post, especially the answer where you see that i = i + 1 and i += 1 are not the same for i = i ?? 1 and i ??= 1 – Samuel Mar 17 '09 at 20:08
  • will do. I did search for an existing topic, but I'm guessing that stackoverflow's search replaced my '??' with a wildcard search so it's not easy to find. Thanks for the feedback :) – CodeMonkeyKing Mar 17 '09 at 20:11
  • 3
    A null coalescing peoperty (dot) operator "?." for returning null when dereferencing a null object (calling a method or property) would be above this on my wish list. That is, string dogOwnerName = Dog?.Owner?.Name which would be null if the dog was null, or if it had no owner. – Anders Forsgren Apr 22 '13 at 09:11
  • 5
    **Update 2017**: Null-coalescing assignment is an active proposal under consideration by the C# Language Design Team. Please vote or join the discussion at https://github.com/dotnet/csharplang/issues/34 – Colonel Panic Jun 13 '18 at 15:46
  • 1
    Roslyn seems to be moving ahead with this feature: https://github.com/dotnet/roslyn/pull/36329 – Esme Povirk Jul 15 '19 at 19:30
  • Oct 2019 Update: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/null-coalescing-assignment – Stuart Aitken Oct 15 '19 at 05:59
  • Update 2020: Arrived in PHP 7.4 upon my proposal https://stackoverflow.com/questions/59102708/what-is-this-new-null-coalescing-assignment-operator-in-php-7-4 – Midori Kocak Jan 13 '20 at 09:31

7 Answers7

15

Other programming languages like Ruby use this quite frequently:

something ||= Something.new
Bob Aman
  • 32,336
  • 9
  • 69
  • 95
8

If 'something' is a private field for a property accessor, you can do the following....this would perform the assignment if the field is found to be null.

private Something something;
public Something Something
{
    get
    {
        return something ?? (something = new Something());
    }
}
phil
  • 830
  • 1
  • 10
  • 20
  • This does not address the intent of the operator. The intent of my asking the question was to ask if there were plans to incorporate a ??= operator and would that make sense. The code example above does not streamline code and has to be written for each case. – CodeMonkeyKing Apr 22 '13 at 17:01
2

I actually implemented that for PHP7. It's now at RFC proposal stage. https://github.com/php/php-src/pull/1795

Actually it was a question about null coalescing operator in PHP7. Check this:

$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’;
I want to check if some var is null and if the same var is null set the same var to ‘value’.

Hence I am repeating the same variable after the equal operator, this does not feels right.

So I feel that we need another operator like “??=“ similar to +=;

$this->request->data['comments']['user_id’] ??= ‘value’. So if the var is null it’s set to ‘value’ and else stays the same.

In that pull request I tried to implement this.

$ sapi/cli/php -r '$num = null;$num ??= 5; echo $num;'
5
Midori Kocak
  • 141
  • 1
  • 7
2

I'm not necessarily against the operator, however, that kind of variable reuse just doesn't feel like the "right way" to me. Bugs that should have been obvious NULL pointers end up populated with unexpected data and work in some unexpected way, etc....

overslacked
  • 4,077
  • 23
  • 28
1

I would say that the ?? operator and more pertinently the above pattern is not so common and so a new operator is overkill.

Jeff Yates
  • 60,114
  • 20
  • 136
  • 187
Mohit Chakraborty
  • 1,251
  • 8
  • 8
1

As ?? is shorthand for using the ternary operator in a fashion similar to:

(myObject != null) ? myObject : somethingElse;

rather than shorthand for an arithmetic operation, I don't think a ??= operation is a good fit. It's a conditional operator.

Jeff Yates
  • 60,114
  • 20
  • 136
  • 187
  • if you want to check the null condition of a variable's state and assign it to itself only in the case of null, it is a good fit. e.g. `_date = _date ?? value;` – Rice Mar 01 '18 at 19:31
-1

I'm honestly torn on the =++ operator in the first place, and it's usage is rather widespread and common. From a clarity point of view, the extra couple of characters you need type probably isn't worth the change to the language to add a ??=. If this was a vote, I'd vote against that change to the language (good question, bad idea. :-)

I haven't tried this, but could you map ??= to a macro in visual studio that just did the rewrite for you?

Walden Leverich
  • 4,318
  • 2
  • 20
  • 30
  • 4
    There is no `=++` operator. I assume you mean the `+=` compound assignment operator. In that case then there is a reason for its inclusion as `a += b` is not just shorthand for `a = a + b`. Take a look at https://msdn.microsoft.com/en-us/library/aa691316%28v=vs.71%29.aspx as a starting place for further info. – 0b101010 Mar 27 '15 at 16:47
  • in the language it will be just a syntactic sugar, it means when compile, the compiler will "expand" that in the normal one, like a lot of other operator. It is not such a bad request. Anyway the idea of VS macro is good as well, but not the real answer. – Raffaello Oct 27 '16 at 11:30