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
3
votes
3 answers

Non-repetitive way of saying: access this object's member unless the object is null

Let's say I have a set of cars, where each car has a steering wheel. I'd like to write a line of code that looks for a car in the set and returns its steering wheel, or returns null if the car isn't in the set. Something like this: Car found = //…
Joe
  • 3,804
  • 7
  • 35
  • 55
2
votes
9 answers

C# coalesce operator Throws

I have a class with a string property. I use the coalesce operator when reading from it as it might be null, but it still throws me an NullRefrenceExeption. string name = user.Section.ParentSection.Name ?? string.Empty; To be more specific, its the…
2
votes
1 answer

Using null coalescing operator with a comparison

Given the method: public static bool IsDateValid(DateTime? date) { if (date.HasValue ? date.GetValueOrDefault() < MinDate : false) { return false; } return date.GetValueOrDefault() < MaxDate; } Is it possible to rewrite the…
arch-imp
  • 217
  • 3
  • 12
2
votes
1 answer

Javascript Logical OR operator in return statement of a function

I am using Vuetify, specifically the v-text-field from inside v-form. Each of these v-text-fields has a property called rules, used for validation. That property accepts an array with a bunch of functions. This is where I've stumbled over a…
2
votes
2 answers

How to configure CRA with TS to support nullish-coalescing-operator

So i started a new CRA project and I'm using the TS beta to get some sweet features like the chaining operator, but i also want to use nullish-coalescing-operator ifExists ?? elseUseThis Unfortunately it didn't work out of the box and told me to…
Sophie McCarrell
  • 2,831
  • 8
  • 31
  • 64
2
votes
0 answers

How can I use a coalesce operator in a DRY way; when assigned value is null, keep own value

I am sending value in object orderContract which will replace values of entity if contract value is not null but if contract property is null it will retain its own value. Currently I have used coalesce operator as in 2nd line of code. Is there a…
Manjay_TBAG
  • 2,176
  • 3
  • 23
  • 43
2
votes
5 answers

Best practice: how to increment not existing element of array

I want to increment a value of an array, which is potentially not existing yet. $array = []; $array['nonExistentYet']++; // Notice Problem This leads to a NOTICE. Attempt I found a way to do this, but its kinda clunky: $array =…
SirPilan
  • 4,649
  • 2
  • 13
  • 26
2
votes
3 answers

How to use null-coalescing operator with ActionResult ASP.NET Core 2.1

Can someone explain me why I'm getting an error on the null-coalescing on the following method: private readonly Product[] products = new Product[]; [HttpGet("{id}")] public ActionResult GetById(int id) { var product =…
Wadjey
  • 145
  • 13
2
votes
3 answers

How to correctly do Nil Coalescing in Swift?

Supposed to do like this var inputField = UITextField() let defaultText = "PLACEHOLDER" let newText = inputField.text!.isEmpty ? defaultText : inputField.text! let newText2 = inputField.text ?? defaultText newText works, and outputs PLACEHOLDER.…
2
votes
3 answers

How to translate "var a = collection?.First()?.somePropA ?? new A();"

What is the purpose of single question mark on the right side of assignment in expression with null-coalescing operator? Example: var a = collection?.First()?.somePropA ?? new A();
andreikashin
  • 1,528
  • 3
  • 14
  • 21
2
votes
2 answers

Null Coalesce with colors

protected override void OnEnter(EventArgs e) { // this.Font = new Font(this.Font, FontStyle.Italic); base.BackColor = _colors.SelectedBackColor ?? base.BackColor; base.ForeColor = _colors.SelectedForeColor ??…
DidIReallyWriteThat
  • 1,033
  • 1
  • 10
  • 39
2
votes
2 answers

Why doesn't the null coalescing operator work on my nullable int when I convert it to a string?

At first I tried to write an If-Then-Else statement using a ternary operator. It works fine then Just out of curiosity I decided to write the same code using a null-coalescing operator but it doesn't work as expected. public…
siamak
  • 669
  • 1
  • 10
  • 28
1
vote
10 answers

Function to check a null string property?

I have an object with string properties, for example, a Patient object with weight and height. But when the property is null, my code fails when I try to use it, as the property is set to null. I'm trying to create a function that checks if the…
Giselle W
  • 25
  • 1
  • 3
1
vote
2 answers

Is there a nice one-liner to provide a default if a value is None?

There is a possibility that I am searching under wrong keywords, but I can't find the answer to the question: Is there a nice one-liner to provide a default value if the variable is NoneType? For example: def validate_quantity(self, value): …
skelaw
  • 199
  • 4
  • 16
1
vote
3 answers

How does the null check operator work in Dart

I'm using this repository to familiarize myself with Amazon's Cognito user system. In the file lib/screens/signup_screen.dart, starting at line 27 there is this piece of code: TextFormField( keyboardType:…
Anteino
  • 1,044
  • 7
  • 28