Questions tagged [null-coalescing-operator]

The null-coalescing operator (?? in PHP 7) is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.

A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable value type is currently undefined, an InvalidOperationException exception will be thrown.

For more information see MSDN.

263 questions
1977
votes
18 answers

What do two question marks together mean in C#?

Ran across this line of code: FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); What do the two question marks mean, is it some kind of ternary operator? It's hard to look up in Google.
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
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…
573
votes
5 answers

Curious null-coalescing operator custom implicit conversion behaviour

Note: this appears to have been fixed in Roslyn This question arose when writing my answer to this one, which talks about the associativity of the null-coalescing operator. Just as a reminder, the idea of the null-coalescing operator is that an…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
551
votes
14 answers

PHP short-ternary ("Elvis") operator vs null coalescing operator

Can someone explain the differences between ternary operator shorthand (?:) and null coalescing operator (??) in PHP? When do they behave differently and when in the same way (if that even happens)? $a ?: $b VS. $a ?? $b
490
votes
12 answers

Is there a Python equivalent of the C# null-coalescing operator?

In C# there's a null-coalescing operator (written as ??) that allows for easy (short) null checking during assignment: string s = null; var other = s ?? "some default value"; Is there a python equivalent? I know that I can do: s = None other = s if…
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
247
votes
5 answers

is there a Java equivalent to null coalescing operator (??) in C#?

Is it possible to do something similar to the following code in Java int y = x ?? -1; More about ??
Nikita Ignatov
  • 6,872
  • 2
  • 34
  • 34
208
votes
12 answers

?? Coalesce for empty string?

Something I find myself doing more and more is checking a string for empty (as in "" or null) and a conditional operator. A current example: s.SiteNumber.IsNullOrEmpty() ? "No Number" : s.SiteNumber; This is just an extension method, it's…
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
201
votes
6 answers

Is there a VB.NET equivalent for C#'s '??' operator?

Is there a VB.NET equivalent for C#'s ?? operator?
Nathan Koop
  • 24,803
  • 25
  • 90
  • 125
176
votes
4 answers

An expression tree lambda may not contain a null propagating operator

The line price = co?.price ?? 0, in the following code gives me the above error, but if I remove ? from co.? it works fine. I was trying to follow this MSDN example where they are using ? on line select new { person.FirstName, PetName = subpet?.Name…
nam
  • 21,967
  • 37
  • 158
  • 332
173
votes
16 answers

Unique ways to use the null coalescing operator

I know the standard way of using the null coalescing operator in C# is to set default values. string nobody = null; string somebody = "Bob Saget"; string anybody = ""; anybody = nobody ?? "Mr. T"; // Returns Mr. T anybody = somebody ?? "Mr. T";…
157
votes
14 answers

Null coalescing in powershell

Is there a null coalescing operator in powershell? I'd like to be able to do these c# commands in powershell: var s = myval ?? "new value"; var x = myval == null ? "" : otherval;
Micah
  • 111,873
  • 86
  • 233
  • 325
140
votes
10 answers

Coalesce function for PHP?

Many programming languages have a coalesce function (returns the first non-NULL value, example). PHP, sadly in 2009, does not. What would be a good way to implement one in PHP until PHP itself gets a coalesce function?
mikl
  • 23,749
  • 20
  • 68
  • 89
122
votes
10 answers

What is the proper way to check for null values?

I love the null-coalescing operator because it makes it easy to assign a default value for nullable types. int y = x ?? -1; That's great, except if I need to do something simple with x. For instance, if I want to check Session, then I usually end…
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
107
votes
12 answers

Is there an "opposite" to the null coalescing operator? (…in any language?)

null coalescing translates roughly to return x, unless it is null, in which case return y I often need return null if x is null, otherwise return x.y I can use return x == null ? null : x.y; Not bad, but that null in the middle always bothers me --…
Jay
  • 56,361
  • 10
  • 99
  • 123
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…
1
2 3
17 18