Questions tagged [null-conditional-operator]

A Safe navigation operator which is used to test for null before performing a member access (?.) or index (?[]) operation.

A Safe navigation operator used to test for null before performing a member access (?.) or index (?[]) operation.

These operators help you write less code to handle null checks, especially for descending into data structures.

C# :

int? length = customers?.Length; // null if customers is null 
Customer first = customers?[0];  // null if customers is null
int? count = customers?[0]?.Orders?.Count();  // null if customers, the first customer, or Orders is null

VB :

Dim length = customers?.Length  ‘’ null if customers is null
Dim first as Customer = customers?(0);  ‘’ null if customers is null
Dim count as Integer? = customers?[0]?.Orders?.Count();  // null if customers, the first customer, or Orders is null

MSDN.

83 questions
123
votes
20 answers

C# elegant way to check if a property's property is null

In C#, say that you want to pull a value off of PropertyC in this example and ObjectA, PropertyA and PropertyB can all be null. ObjectA.PropertyA.PropertyB.PropertyC How can I get PropertyC safely with the least amount of code? Right now I would…
Jon Kragh
  • 4,529
  • 5
  • 26
  • 26
65
votes
2 answers

Trying to understand ?. (null-conditional) operator in C#

I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { …
Dee J. Doena
  • 1,631
  • 3
  • 16
  • 26
51
votes
2 answers

C# Error with null-conditional operator and await

I'm experiencing an interesting System.NullReferenceException whilst using the new null-conditional operator in C#. The following code gives me a NullReferenceException if "MyObject" is null: await this.MyObject?.MyMethod() I would've expected that…
45
votes
7 answers

Using the null-conditional operator on the left-hand side of an assignment

I have a few pages, each with a property named Data. On another page I'm setting this data like this: if (MyPage1 != null) MyPage1.Data = this.data; if (MyPage2 != null) MyPage2.Data = this.data; if (MyPage3 != null) MyPage3.Data =…
diiN__________
  • 7,393
  • 6
  • 42
  • 69
33
votes
2 answers

Is C# 6 ?. (Elvis op) thread safe? If so, how?

Apologies in advance: this question comes from a hard-core, unreformed C++ developer trying to learn advanced C#. Consider the following: if (myUserDefinedObject != null) { myUserDefinedObject.ToString(); } This is obviously not thread safe. On…
Dick Bridges
  • 331
  • 3
  • 4
29
votes
3 answers

Does the "?." operator do anything else apart from checking for null?

As you might know, DateTime? does not have a parametrized ToString (for the purposes of formatting the output), and doing something like DateTime? dt = DateTime.Now; string x; if(dt != null) x = dt.ToString("dd/MM/yyyy"); will throw No…
iuliu.net
  • 6,666
  • 6
  • 46
  • 69
23
votes
4 answers

C# Safe navigation operator - what is actually going on?

I've been following the safe navigation operator feature added in C#6 with some interest. I've been looking forward to it for a while. But I'm finding some different behavior than I expected. I'm realizing I really don't understand how it…
recursive
  • 83,943
  • 34
  • 151
  • 241
21
votes
4 answers

Why do I have to place () around null-conditional expression to use the correct method overload?

I have these extension methods and enum type: public static bool IsOneOf(this T thing, params T[] things) { return things.Contains(thing); } public static bool IsOneOf(this T? thing, params T[] things) where T : struct { return…
Kit
  • 20,354
  • 4
  • 60
  • 103
20
votes
3 answers

Using the Null Conditional Operator to check values on objects which might be null

I've been playing with C# 6's Null Conditional Operator (more info here). I really like the syntax and I think it makes the code much more readable however I think it is questionable as to what exactly the code is going to do when you come across…
Danny Lager
  • 371
  • 1
  • 4
  • 17
13
votes
1 answer

Using null-conditional bool? in if statement

Why this code works: if (list?.Any() == true) but this code doesn't: if (list?.Any()) saying Error CS0266 Cannot implicitly convert type 'bool?' to 'bool' So why is it not a language feature making such an implicit conversion in the if statement?
Centro
  • 3,892
  • 2
  • 25
  • 31
11
votes
2 answers

C# 6.0 multiple identical null conditional operator checks vs single traditional check

Which out of the following two equivalent ways would be best for the null conditional operator in terms of primarily performance and then ease of use or clarity etc.? This: idString = child?.Id; fatherName = child?.Father?.Name; motherName =…
GDS
  • 1,337
  • 12
  • 18
9
votes
4 answers

How can I use the Nullable Operator with the Null Conditional operator?

Old Way int? myFavoriteNumber = 42; int total = 0; if (myfavoriteNumber.HasValue) total += myFavoriteNumber.Value *2; New way? int? myFavoriteNumber = 42; total += myFavoriteNumber?.Value *2; //fails
8
votes
4 answers

Null-conditional boolean in if statement

I have an event which returns a boolean. To make sure the event is only fired if anyone is listening, i call it using the null-conditional operator (questionmark). However, this means that I have to add the null-conditional operator to the returned…
Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
8
votes
2 answers

null-conditional operator doesn't work with Func inside a generic method

Is this a compiler bug or is there a specific chosen reason why the null-conditional operator doesn't work with Func inside of generic methods? To give an example the following doesn't compile public static T Test(Func func) { return…
Bauss
  • 2,767
  • 24
  • 28
7
votes
3 answers

Null Conditional in Powershell?

C# and other languages have null-conditionals usually ?. A?.B?.Do($C); Will not error out when A or B are null. How do I achieve something similar in powershell, what's a nicer way to do: if ($A) { if ($B) { $A.B.Do($C); } }
RaGe
  • 22,696
  • 11
  • 72
  • 104
1
2 3 4 5 6