null-conditional-operator Questions

2

Solved

I'm using Newtonsoft to deserialize an known JSON object and retrieve some values from it if they're present. The crux is there is that object structure may keep changing so I'm using dynamic to tr...
Strother asked 8/4, 2022 at 17:41

20

Solved

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...
Aggappera asked 12/8, 2010 at 13:40

7

Solved

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...
Aurelioaurelius asked 9/3, 2016 at 9:3

4

Solved

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 r...
Adley asked 28/8, 2015 at 1:13

3

Solved

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 ($...
Marconigraph asked 25/10, 2019 at 14:47

3

Solved

The null-conditional operator is very useful when the method belongs to the object in question, but what if the object in question is an argument? For example, can this be shortened? var someList ...
Garey asked 8/4, 2018 at 8:57

2

Solved

Consider the following code: IEnumerable<int> xx = null; var tt = xx?.Where(x => x > 2).Select(x => x.ToString()); It assigns null to tt. The question is: why does it work properl...
Expendable asked 16/2, 2018 at 17:1

1

Solved

We encountered unexpected behavior with the null conditional operator if the variable value is Nothing. The behavior of the following code keeps us a bit confused Dim l As List(Of Object) = Meth...
Enyedy asked 17/10, 2017 at 10:38

3

Solved

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 ex...
Concha asked 21/8, 2017 at 12:3

1

Solved

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 f...
Convalescence asked 9/8, 2017 at 17:18

2

Solved

The null conditional operator can be used to skip method calls on a null target. Would the method arguments be evaluated or not in that case? For example: myObject?.DoSomething(GetFromNetwork());...
Garrow asked 5/8, 2017 at 12:15

4

Solved

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 ...
Agace asked 13/6, 2017 at 9:34

1

Solved

Before C# 6, I would write code to dispose of an object like: if (_odbcConnection != null) { _odbcConnection.Close(); _odbcConnection.Dispose(); _odbcConnection = null; } With 6, I can write mu...
Unconformity asked 7/2, 2017 at 15:12

2

Solved

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 pu...
Flatware asked 13/1, 2017 at 14:39

2

Solved

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; ...
Mister asked 6/12, 2016 at 9:56

1

Solved

I have the following line of code: user.Exists = await this.repository?.Exists(id); Exists on the left hand side is a property of the User class. Its type is just bool, not bool?. The Exists met...
Hyponasty asked 11/11, 2016 at 23:12

2

Solved

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) { System.Console.Writ...
Corazoncorban asked 29/6, 2016 at 15:27

1

Solved

Observation: If text is null, this method returns True. I expected False. return text?.IndexOf('A') != -1; When I reflect the above line using ILSpy (or inspect the IL), this is the generated co...
Mcnutt asked 7/6, 2016 at 0:51

4

Solved

I have these extension methods and enum type: public static bool IsOneOf<T>(this T thing, params T[] things) { return things.Contains(thing); } public static bool IsOneOf<T>(this T? ...
Futuristic asked 24/5, 2016 at 19:42

4

Old Way int? myFavoriteNumber = 42; int total = 0; if (myfavoriteNumber.HasValue) total += myFavoriteNumber.Value *2; New way? int? myFavoriteNumber = 42; total += myFavoriteNumber?.Value *2;...

2

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.To...
Maraschino asked 3/3, 2016 at 23:7

3

Solved

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 = ...
Melissa asked 29/1, 2016 at 10:24

6

Solved

I have been looking around finding the best possible option for handling null objects in calling a method (or method chain). It is our common practice to check with if condition: if ( customObje...
Aftergrowth asked 31/8, 2014 at 23:50
1

© 2022 - 2024 — McMap. All rights reserved.