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
1
vote
3 answers

How to print the null values if the dictionary has it using LINQ

I have dictionary with the following keys and values. I am trying to print the dictionary, but nothing prints where there is a null value. How do I print "null" in the output? Dictionary dic1 = new Dictionary(); …
1
vote
2 answers

Null coalescing string and conditional string via logical OR-operator results in number

Null coalescing by ORing a string (via PRIORITIES[NUM_TO_PRIORITY[priorityNum]] where priorityNum is input) and a string (via conditional Object.values(PRIORITIES).includes(priorityNum) ? priorityNum : PRIORITIES.low) should output a string yet…
1
vote
1 answer

Null-coalescing operator in conjuction with JObject

I have below code written to read JArray from a given JObject. To my understanding, when the value of "tags" in JObject is null, IEnumerable should be initialized as empty. IEnumerable tags = eventPayload?["tags"]?.Values() ??…
Evan Park
  • 528
  • 1
  • 6
  • 20
1
vote
3 answers

Optional chaining and coalescing incl. function arguments

Optional chaining lets us make decisions on the existence of objects: var text : String? let len = text?.lengthOfBytes(using: .utf8) ?? 0 Which will always set len to an integer. Is something similar possible for non-optional function arguments?…
Eiko
  • 25,601
  • 15
  • 56
  • 71
1
vote
1 answer

PHP 5 login "syntax error, unexpected '?' " while using ?? operator

This code is part of the login form, and I want to make sure that the field is not blank. When I am using this line of code I get the following error: syntax error, unexpected '?' if(is_blank($admin['username'])) { $errors[] = "Username…
1
vote
2 answers

Access Optional property in multiple function for calculations - Swift

I have a NSObject Subclass. Say CityWalks class CityWalks{ var totalCount:Int? } How do I use this property further? Should I check the nil coalescing every time this value is accessed. example: let aObject = say in one fucntion (function1())…
NNikN
  • 3,720
  • 6
  • 44
  • 86
0
votes
1 answer

SwiftUI error Value of optional type 'UIImage?' must be unwrapped to a value of type 'UIImage'

I'm trying to show a placeholder image in the case there has not been a picture made: // Function to create a photo button and image placeholder func photoButton(title: String) -> some View { VStack { // Image placeholder …
0
votes
2 answers

assigned null value in Null-coalescing assignmen C#

If the phrase ?? = in C # is for assigning null then why is the value assigned in this example? IList list = new List() {"cat" , "book" }; (list ??= new List()).Add("test"); foreach (var item in list) { …
0
votes
1 answer

Check if an a propery of an object from an array is contained in another array of object

Let's say I have these arrays of object selectedNames = [ {label: 'Nicolas', value:'Nicolas'}, {label: 'Michael', value:'Michael'}, {label: 'Sean', value:'Sean'}, {label: 'George', value:'George'} ] selectedWhatever = [ {label:…
palnic
  • 386
  • 1
  • 4
  • 13
0
votes
2 answers

Using c# NULL coalescing operator with NOT NULL Scenarios

I am populating a Class using LINQ and LinkUrl is a string property of my class.I need to set a value only if a property is not null , other wise no need to assign any values Currently The conditional operator ?: is used var formattedData =…
Sebastian
  • 4,625
  • 17
  • 76
  • 145
0
votes
0 answers

Why does (Guid?)null ?? default return empty guid

Output from VS Immediate Window default(Guid?) => null (Guid?)null == default => true (Guid?)null ?? default => {00000000-0000-0000-0000-000000000000} I expected the last to be null and not Guid.Empty. So why is it this way?
0
votes
2 answers

Is the c# coalescence the same as an if statement when setting to null?

Are these two statements the same? if (dep.BirthDate.HasValue) { myObj.GetType().GetProperty("birthdate").SetValue(myObj, (DateTime)dep.BirthDate, null); } myObj.GetType().GetProperty("birthdate").SetValue(myObj, dep.BirthDate ?? null, null); I…
chuckd
  • 13,460
  • 29
  • 152
  • 331
0
votes
0 answers

is this equivalent ? (int) listofobj?.Count() to (listofobj == null)?0:listofobj.Count();

i have List listOfObj; i have some property where i'd like to return number of objects i tried it like get => (int)listOfObj?.Count(); but app crashes with System.Reflection.TargetInvocationException somewhere in xamarin.forms page…
ish1313
  • 321
  • 3
  • 6
0
votes
3 answers

How to efficiently display text instead of null when binding in vue.js?

In the following code site can be null, but if it is there, then company will not be null. How can I efficiently display a "-" when the site is null that scales well for 1000's of these rows? …
Colton Scottie
  • 807
  • 1
  • 8
  • 22
0
votes
1 answer

Missing Dart null checking shorthand?

I know that Dart has the null checking shorthands x = y ?? z; and x = y?.z; But what if I have a statement like x = (y != null) ? SomeClass(y) : z; I don't want to pass null to the SomeClass constructor - so in that case I want x to be set to z…
Magnus
  • 17,157
  • 19
  • 104
  • 189