null-propagation-operator Questions

4

Solved

The line price = co?.price ?? 0, in the following code gives me the above error, but if I remove ? from co.? it works fine. I was trying to follow this MSDN example where they are using ? on line s...

4

Solved

The new C# 6.0 null-conditional operator is a handy vehicle for writing more concise and less convoluted code. Assuming one has an array of customers, then you could get null instead of a length if...
Kajdan asked 5/5, 2016 at 0:31

3

Solved

I have the following code: await _user?.DisposeAsync(); Visual Studio highlights this code, saying 'Possible NullReferenceException' by the way, without await Visual Studio doesn't show this wa...

1

Solved

Given this code: private void TryIt(Dictionary<int, int> myDict) { if (myDict?.TryGetValue(1, out int myValue) ?? false) { Console.Out.WriteLine(myValue); // <-- Error CS0165 } ...
Conventual asked 10/4, 2019 at 22:26

3

Solved

Let's assume I have a class that has a property of type Dictionary<string,string>, that may be null. This compiles but the call to TryGetValue() could throw at a NullRef exception at runtime...
Vulpecula asked 6/12, 2017 at 22:49

4

Solved

Reading a lot about the Null propagation operator ?., I found no answer whether it is helpful in the following scenario. Code that throws: int[] values = null; foreach ( var i in values ) // Thr...
Plaza asked 30/12, 2014 at 10:26

2

Solved

One of my favorite C# features added is the "null-propagation" in CS6. This has cleaned up so much code for many of us. I came across a situation where this doesn't appear to be possible. I am no...
Discriminating asked 1/8, 2017 at 22:32

1

Solved

After scratching my head for the better part of the day, I stumbled upon a very weird issue with .NET code that is compiled using .NET Native (used for Windows UWP apps). The following code works ...

2

Solved

I'm specifically calling attention to null-propagation as it pertains to bool? and the use of a bool returning method. For example, consider the following: public static bool IsAttributedWith<T...
Specimen asked 20/1, 2017 at 2:16

1

Solved

After seeing a similar question, I was wondering if the following expression ... if (attribute != null && attribute.Description == input) ... would behave (almost) identical, to followin...
Ealasaid asked 5/12, 2016 at 14:48

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

Solved

var result = myObject?.GetType(); In this scenario what would be the value of Result if myObject is null?
Endicott asked 3/12, 2015 at 1:29

3

Solved

This question has been completely overhauled in the interest of being thorough in explanation. I have noticed what appears to be quite a poor limitation of the null propagation operator in C# 6.0 ...
Potbelly asked 11/9, 2015 at 8:42

3

Solved

Trying to make Feature generic and then suddenly compiler said Operator '?' cannot be applied to operand of type 'T' Here is the code public abstract class Feature<T> { public T Value ...
Piragua asked 15/9, 2015 at 7:49

3

Solved

I stumbled across an interesting site, where some of the new (proposed) features of C# 6.0 are addressed. You may read it here: Probable C# 6.0 features. What I find particular interesting is the ...
Whitehorse asked 13/1, 2015 at 12:11

1

I am running a .NET 4.5 project in VS 2013. Why is the following code in error? var w = Request.Properties["MS_HttpContext"] as System.Web.HttpContextWrapper; string IP = w?.Request.UserHostAddres...
Cathe asked 8/4, 2015 at 14:47

1

Solved

I often use null propagating operator in my code because it gives me more readable code, specially in long queries I don't have to null-check every single class that is used. The following code t...
1

© 2022 - 2024 — McMap. All rights reserved.