Questions tagged [null-coalescing]

The concept of setting a default value if a condition evaluates to null.

In C#, the ?? operator is known as the null coalescing operator. It returns the first operand unless it's null, in which case it returns the second operand:

string foo = null;
Console.WriteLine(foo ?? "bar"); // Outputs "bar"

foo = "baz";
Console.WriteLine(foo ?? "bar"); // Outputs "baz"

There are a few equivalent ideas in other languages but not all languages have an equivalent operator. As of Perl 5.10, one can use the // operator to set a default value if the condition evaluates to undef.

69 questions
1557
votes
19 answers

Is there a "null coalescing" operator in JavaScript?

Is there a null coalescing operator in Javascript? For example, in C#, I can do this: String someString = null; var whatIWant = someString ?? "Cookies!"; The best approximation I can figure out for Javascript is using the conditional operator: var…
272
votes
22 answers

Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?

I'll explain by example: Elvis Operator (?: ) The "Elvis operator" is a shortening of Java's ternary operator. One instance of where this is handy is for returning a 'sensible default' value if an expression resolves to false or null. A…
tiagomac
  • 2,935
  • 2
  • 15
  • 6
89
votes
3 answers

What is the ?[]? syntax in C#?

While I was studying the delegate which is actually an abstract class in Delegate.cs, I saw the following method in which I don't understand Why the return value uses ? though it's already a reference(class) type ?[]? meaning on the…
88
votes
7 answers

What does a double question mark do in C#?

Possible Duplicates: ?? Null Coalescing Operator --> What does coalescing mean? What do two question marks together mean in C#? I couldn't find this question being asked here so I figured I would ask it. What does a double question mark do in…
tom d
  • 1,023
  • 1
  • 8
  • 10
62
votes
11 answers

Is there a shorthand way to return values that might be null?

How can I write a shorthand of the following scenario? get { if (_rows == null) { _rows = new List(); } return _rows; }
ErrorAgain
  • 755
  • 5
  • 7
34
votes
5 answers

c# shorthand for if not null then assign value

Is there any shorthand in c# now that will cutdown the following code: var testVar1 = checkObject(); if (testVar1 != null) { testVar2 = testVar1; } In this situation only want to assign testVar2 if testVar1 is not null from the CheckObject()…
Matt
  • 3,305
  • 11
  • 54
  • 98
31
votes
3 answers

Null Coalescing Operator in F#?

When interacting with C# libraries, I find myself wanting C#'s null coalescing operator both for Nullable structs and reference types. Is it possible to approximate this in F# with a single overloaded operator that inlines the appropriate if case?
jbtule
  • 31,383
  • 12
  • 95
  • 128
21
votes
3 answers

Return a default value if single row is not found

I have the following select statement to grab the next scheduled item for a stream. If there is no matching row, I want it to return a default value. Here's the SQL that I'm using: SELECT `file` FROM `show`, `schedule` WHERE `channel` = 1 AND…
Fibericon
  • 5,684
  • 12
  • 37
  • 64
11
votes
2 answers

Postgresql COALESCE performance problem

I have this table in Postgresql: CREATE TABLE my_table ( id bigint NOT NULL, value bigint, CONSTRAINT my_table_pkey PRIMARY KEY (id) ); There are ~50000 rows in my_table. The question is, why the query: SELECT * FROM my_table WHERE id =…
Tair
  • 3,779
  • 2
  • 20
  • 33
11
votes
1 answer

None propagation in Python chained attribute access

Is there a null propagation operator ("null-aware member access" operator) in Python so I could write something like var = object?.children?.grandchildren?.property as in C#, VB.NET and TypeScript, instead of var = None if not myobject\ …
11
votes
3 answers

Null-coalescing out parameter gives unexpected warning

Using this construct: var dict = new Dictionary(); var result = (dict?.TryGetValue(1, out var value) ?? false) ? value : "Default"; I get an error saying CS0165 use of unassigned local variable 'value' which is not what I expect. How…
Jakob Möllås
  • 4,239
  • 3
  • 33
  • 61
10
votes
5 answers

What do you think about ??= operator in C#?

Do you think that C# will support something like ??= operator? Instead of this: if (list == null) list = new List(); It might be possible to write: list ??= new List(); Now, I could use (but it seems to me not well readable): list =…
TN.
  • 18,874
  • 30
  • 99
  • 157
8
votes
2 answers

null-conditional operator doesn't work with Func inside a generic method

Is this a compiler bug or is there a specific chosen reason why the null-conditional operator doesn't work with Func inside of generic methods? To give an example the following doesn't compile public static T Test(Func func) { return…
Bauss
  • 2,767
  • 24
  • 28
8
votes
4 answers

C# Null coalesce with LINQ

I have 2 classes which looks like this: class Widget { string Selected { get; set; } List
Shevek
  • 3,869
  • 5
  • 43
  • 63
7
votes
2 answers

?? or .GetValueOrDefault()

I have properties type int? and decimal? that are being used in a calculation. If the value of any of them is null it has to default to 0. I'm trying to decide between using null-coalescing or GetValueOrDefault() which will also default to 0 if a…
Roman Svitukha
  • 1,302
  • 1
  • 12
  • 22
1
2 3 4 5