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

empty text input means empty $_POST[] element? (Null coallesce)

So my question is: If I don't write anything in an input in a simple form with post method, when I receive the $_POST array in my action url, the $_POST["test"] exists as empty string ($_POST["test"] => ""). So I…
franlol
  • 51
  • 7
0
votes
1 answer

Operating on undefined variable - null coalescing

function add(num) { var sum; for (var i = 1; i < num + 1; i++){ sum = (sum || 0) + i; }; return sum; } add(9); What is a keyword to describe the behavior for defining sum when it gets added to "i" in the for loop? Is this a…
noob-in-need
  • 861
  • 1
  • 7
  • 13
0
votes
2 answers

nil coalescing: Using optional types as default value in Swift

I have this: let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) dispatch_async(queue, { if let data = NSURLConnection.sendSynchronousRequest(self.buildRequestForVenueLocation(location, identifier),…
Nilzone-
  • 2,766
  • 6
  • 35
  • 71
0
votes
2 answers

SQL Server convert NULL to empty string in select *

I need to execute the following: SELECT * FROM [MY_TVF](9186) FOR XML AUTO, ELEMENTS And replace all NULL values with an empty string '' to include them in the XML. I know I can spit out the elements with an xsi:nil="true" attribute by setting…
MarioDS
  • 12,895
  • 15
  • 65
  • 121
0
votes
3 answers

How to concat strings with space delimiter where each string is nullable?

I have a table with multiple nullable string fields. For reporting I would like to combine them into a single string that is delimited with a space for every field. The right side must be trimmed. Test data: DECLARE @test TABLE ( f1…
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
-1
votes
1 answer

Which is more efficient for initialising a property, null checking in an if or coalescence?

I have been working to make some code more efficient, and I am wondering now which pattern is the more efficient. The solution has code in both VB.NET and C# for legacy reasons. I have put in the VB and C# versions of the two approaches that we…
Paul Evans
  • 21
  • 5
-1
votes
4 answers

?? Operator (C#) for double returns incorrect result

Using C#, any idea why the following method returns 57.999999999999993 (instead of 58)? double Test_Null_Coalescing_Operator() { double? x = 0.58; return ((x ?? 0) * 100); } //returns 57.999999999999993 (instead of 58)
Samad
  • 1
-2
votes
1 answer

Ternary assignment without "else" expression

Is it possible to use a shorthand for ternary assignment expressions of the type boolean x = false; // ... code ... a = x ? b : a; // Assigning a to a is pointless a += x ? 1 : 0; // Adding 0 to a is pointless I'm thinking something along the…
Magnus
  • 17,157
  • 19
  • 104
  • 189
-3
votes
0 answers

Is there a way to return from a javascript function using the nullish coalescing operator?

Is there a way to return from a function like this (example using express.js): module.exports = (req, res) => { User.findById(req.session.userId, (error, user)=> { (error||!user) && return res.redirect('/') }) } Or am I confined to…
ttat2g
  • 45
  • 6
1 2 3 4
5