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)
returnsfalse
because left-side
operand isnull
and right side operand is executed.(emptyCollection?.Any() ?? false)
also returnsfalse
, left-side
operand isn'tnull
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
C# Generics Compared to C++ Templates
How to Display the Output of SQL "Print" Command in C#
C# List - Removing Items While Looping/Iterating
Solving a Timeout Error for SQL Query
Multiple Producers, Single Consumer
Incorrect Syntax Near the Keyword 'User'
How to Pass Current User Information to All Layers in Ddd
Creating Safe SQL Statements as Strings
How to Get Float Value with SQLdatareader
SQL Server Blocked Access to Procedure 'Sys.Sp_Oacreate' of Component 'Ole Automation Procedures'
Is Tls 1.1 and Tls 1.2 Enabled by Default for .Net 4.5 and .Net 4.5.1
Entity Framework - Capitalizing First Property Name Letter
Internal Server Error While Running a Simple .Net Core MVC Application on Ubuntu 16.04
Why Can't I Reference System.Componentmodel.Dataannotations
How to Rewrite Complicated Lines of C++ Code (Nested Ternary Operator)
How to Pass Variable into SQLcommand Statement and Insert into Database Table
How Could the New Async Feature in C# 5.0 Be Implemented with Call/Cc