C# Null Coalescing Operator Equivalent for C++

C# null coalescing operator equivalent for c++

There isn't a way to do this by default in C++, but you could write one:

in C# the ?? operator is defined as

a ?? b === (a != null ? a : b)

So, the C++ method would look like

Coalesce(a, b) // put your own types in, or make a template
{
return a != null ? a : b;
}

C# Null Coalescing Operator Behaves Differently in Conjunction with OR Operator Between Null and Empty Collection

According to operators precedence, conditional OR operator || has higher priority than null-coalescing operator ??.

According to null-coalescing operator specs:

The ?? operator doesn't evaluate its right-hand operand if the
left-hand operand evaluates to non-null.

So, in your code adding a parenthesis is needed to make the correct order of execution and ?? operator evaluation.

Without parenthesis this nullCollection?.Any() returns null, therefore ?? operator returns result of false || true (because of priority, mentioned above), which is always true.

emptyCollection?.Any() result is not null and returns false, therefore right side false || true isn't evaluated, compiler is smart enough to understand it.

Adding a parenthesis force the compiler to perform the expected execution of operators:

  • (nullCollection?.Any() ?? false) returns false because left-side
    operand is null and right side operand is executed.
  • (emptyCollection?.Any() ?? false) also returns false, left-side
    operand isn't null and right side isn't executed.

Logical OR for false || true returns true in both cases and you'll see a desired behavior

Practical purpose of the null-coalescing assignment operator in C#?

A real world example would be lazy loading a backing field on first access when that backing field is null. Something like this:

private string _dbQuery;
private string DbQuery => _dbQuery ??= GetQuery(queryName);

VB equivalent of C# null coalescing operator - specific case

C-heritage languages (which includes C#) have historically treated assignment as an expression which means that it has a result. Historically, that result is the value that was assigned. This has advantages, which includes allowing more concise multiple assignments and testing results of assignment in conditionals. It also has disadvantages, which include potential confusion of assignment and equality operators in conditionals.

Other languages treat assignment as a statement which means that it does not have a result and cannot be composed into another operation. VB is one of these languages (others include Fortran and Pascal).

Because assignment is a statement in VB, you cannot assign and test in the same operation unless you write your own function to do so. You will have to do your two-line operation, or use the : statement separator to put both statements on one line.

Idle curiosity on VB that relates to language families: pre-.NET, VB had much more in common with Fortran than C. The end block statements follow the same conventions as Fortran, the code editor will recognize EndIf and convert it to End If, the array layout was the same (column-major instead of row-major), and the code editor would recognize the Fortran convention for a double-precision constant e.g. 1.0d0 would be treated as a double 1.0. The last two have been lost with the move to .NET.

Coalesce operator in C#?

Yup:

tb_myTextBox.Text = o.Member ?? "default";

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator



Related Topics



Leave a reply



Submit