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
7
votes
1 answer

Null coalescing on a dict entry in python

So, I'm aware that, in Python I can do this: variable_name = other_variable or 'something else' ...and that that will assign 'something else' to variable_name if other_variable is falsy, and otherwise assign other_variable value to variable. Can I…
Owen C. Jones
  • 1,435
  • 1
  • 11
  • 13
7
votes
2 answers

Null coalescing operator (??) with return

I was wondering why it is possible to do this in C# 7.0: int? test = 0; int test2 = test ?? throw new Exception("Error"); ..but not this: int? test = 0; int test2 = test ?? return; Can someone explain that?
Twenty
  • 5,234
  • 4
  • 32
  • 67
7
votes
2 answers

ORACLE SQL: How do I replace NULL with 0 in a Pivot function

How can I replace NULL with 0 in a PIVOT function on ORACLE SQL? This is the query I'm trying to write: SELECT * FROM ( SELECT DISTINCT CUSTOMER_ID AS CUSTOMER_ID, CASE WHEN CATEGORY_CODE = '01' THEN 'CAT 01' WHEN…
MeadMaker
  • 300
  • 1
  • 2
  • 6
6
votes
3 answers

Multi-argument null coalesce and built-in "or" function in Python

Python has a great syntax for null coalescing: c = a or b This sets c to a if a is not False, None, empty, or 0, otherwise c is set to b. (Yes, technically this is not null coalescing, it's more like bool coalescing, but it's close enough for the…
6
votes
1 answer

How to code an intelligent coalesce in Java?

object.getProperty().getSubProperty().getSubSubProperty(); Consider the code above. An object has a property, that has a subProperty, that has a subSubProperty, that can be accessed with getter methods. What can we do in Java to achieve something…
GarouDan
  • 3,743
  • 9
  • 49
  • 75
6
votes
3 answers

Is there an equivalent of isset or a null coalescing operator in SASS?

I want to set a variable only if it hasn't been set. SO... In _layout.scss: $page-width: 1100px; However, in my _module.scss file, I don't want to have a dependency on _layout.scss I'd like to do something like this : $page-width = $page-width ??…
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
6
votes
3 answers

Sending null parameters to Sql Server

I have a SqlCommand object that I'm using to update a database table but it doesn't interpret my null values correctly. Here is the SQL: UPDATE dbo.tbl SET param1 = @param1, param2 = @param2, param3 = @param3, param4 = @param4, param5 = @param5,…
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
5
votes
3 answers

C# coalesce operator doesn't replace a null method return value?

I have this code: MyClass _localMyClass = MyClassDAO.GetMyClassByID(123) ?? new MyClass(); This is the method: public static MyClass GetMyClassByID(int id) { var query = from m in ctx.MyClass where m.MyClassID ==…
5
votes
2 answers

IEnumerable null coalescing Extension

I frequently face the problem to check whether an IEnumerable is null before iterating over it through foreach or LINQ queries, then I often come into codes like this: var myProjection = (myList ?? Enumerable.Empty()).Select(x =>…
digEmAll
  • 56,430
  • 9
  • 115
  • 140
5
votes
1 answer

Is the .NET Reflector unable to reflect over the null-coalescing operator correctly?

I wrote this piece of code: private Queue EnsureQueue() { return _queue ?? (_queue = new Queue(10)); } and the reflector gives me: private Queue EnsureQueue() { if (this._queue == null) { } return (this._queue =…
5
votes
2 answers

Is there a way to implement and make use of a "NOT null coalescing" operator?

Is there a not null coalescing operator in C# which in case could be used such as: public void Foo(string arg1) { Bar b = arg1 !?? Bar.Parse(arg1); } The following case made me think of it: public void SomeMethod(string strStartDate) { …
Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
4
votes
1 answer

Unwrap/coalesce a multi-level optional

I'm trying to write a function to unwrap optionals with an arbitrary number of levels of nesting. Here's the test I'm using: let a: Int??? = 1 let b: Int??? = nil print(a.unwrap(0), b.unwrap(0)) // should print 1, 0 I can get the correct output…
John Montgomery
  • 6,739
  • 9
  • 52
  • 68
4
votes
2 answers

C# type inference ("var") assignment from '??' null-coalescing operator

I've read many of the SO questions on the null coalescing ?? operator but none of them seem to address the following specific issue, which concerns neither nullability (here), operator precedence (here and here) nor especially implicit conversion…
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
4
votes
2 answers

How to use collasce null operator with DbNull.Value?

Well this is one of method to handle DBNull.value, But I want a syntax using null-coalescing operator to handle DBNull.value This will work decimal UnitPrice = row["UnitPrice"] == DBNull.Value ? 0.00m : (decimal)row["UnitPrice"]; Well I have tried…
3
votes
2 answers

Null-coalescing out parameter doesnt give error

This question describes a error related to using Null-coalescing, Null-conditional and out parameter: Null-coalescing out parameter gives unexpected warning The answer details the reasons for this error. But when I copy the example from the question…