❔ ?[] operator for dictionaries
Is not working. It is explained here. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-
Can somebody help?
Member access and null-conditional operators and expressions:
C# operators that you use to access type members or null-conditionally access type members. These operators include the dot operator -
.
, indexers - [
, ]
, ^
and ..
, and invocation - (
, )
.J
Jimmacle•400d ago
it is working exactly as it should
your exception is because the key is not present in the dictionary, not because the dictionary is null
you should use something like
TryGetValue
instead of the indexer if you aren't sure the key existsX
.0xdeadc0de•400d ago
I am not testing against if dictionary is null
J
Jimmacle•400d ago
you are with
_data?["humidity"]
_data?
tests if _data
is null before trying to use the indexer
it has nothing to do with finding keys in the dictionaryX
.0xdeadc0de•400d ago
Okay I see, how to yield null in this situation? Does csharp has such operator?
J
Jimmacle•400d ago
i don't know what you mean by that
the pattern to do this would be something like
K
Kouhai /人◕ ‿‿ ◕人\•400d ago
You can just do
data.TryGetValue("humidity", out var val) ? val : default
X
.0xdeadc0de•400d ago
K
Kouhai /人◕ ‿‿ ◕人\•400d ago
No,
TryGetValue
returns a boolX
.0xdeadc0de•400d ago
Yes
BIA
basically, i am little cat•400d ago
But what if dictionary is null
Then its nullable bool
Isn't it?
K
Kouhai /人◕ ‿‿ ◕人\•400d ago
I don't think they want to test for whether dict is null
X
.0xdeadc0de•400d ago
Correct
K
Kouhai /人◕ ‿‿ ◕人\•400d ago
Oh I guess I was wrong
My bad
X
.0xdeadc0de•400d ago
I mean data can be null, so its nullable bool
using
??
to short-circuit
although this is less readable than I anticipatedK
Kouhai /人◕ ‿‿ ◕人\•400d ago
Yeah, I just thought you wanted to check for existance of a value in a dictionary 😅
You can make it an extension method
BIA
basically, i am little cat•400d ago
You could make an extension method like, GetOrDefault
X
.0xdeadc0de•400d ago
MS should do that :
K
Kouhai /人◕ ‿‿ ◕人\•400d ago
Actually
K
Kouhai /人◕ ‿‿ ◕人\•400d ago
CollectionExtensions.GetValueOrDefault Method (System.Collections.G...
Tries to get the value associated with the specified key in the dictionary.
K
Kouhai /人◕ ‿‿ ◕人\•400d ago
Turns out it's already there
BIA
basically, i am little cat•400d ago
Guh
A
Accord•399d ago
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
52KMembers
View on DiscordWant results from more Discord servers?
More Posts❔ IAsyncEnumerable vs List<T> as parameter in methodI have a method that is overloaded and will take in a List<T> or IAsyncEnumerable. The method will b❔ This code is bugged how can i solve it?```LogiwaEntities1 db = new LogiwaEntities1();
ProductData productData = new ProductData✅ How to add hover effect to WPF button elementThis code I'm using isn't working. When I hover over buttons its a lightblue color.
<Style with or without refso i learned that in c#(or most oop languages) when you create a method that takes an object as a pa✅ .net trying to find non-existent cs files`Error IDE1100 Error reading content of source file 'D:\Dev\C++\Test\TestCS\obj\Debug\net6.0\.❔ WPF - Binding to last child of collectionI have the following setup:
```csharp
public partial class StatusMessageService : ObservableObject
✅ What does WPF want from me?I'm actually so tired of WPF and MVVM and all this contrived fuckin bs you guys
MainWindow control:❔ I apparently have an infinite loop, but I can't fix it to workI've tried a lot of stuff, even a "NewtonJson" fix I saw, but it wouldn't solve the problem because ✅ doing nothingdfg❔ I would appreciate some help.So i have this assignment and in lieu of my grandma who passed this past week i've not had the oppor❔ Discord Bot Service ProblemDoes anyone know how to register that service properly?❔ abstract class vs interface```cs
public interface IFoo
{
protected static string GetFormattedTime() => DateTime✅ Refit / RestLess API SDK StructureHey!
How do I structure an SDK for my API?
Let's say I have 2 resources: Users and Groups.
I coul❔ Best way to display a continually updating list of messages```cs
@inject MerpieClient Client
@inject MerpieHubConnection HubConnection
@inject ILogger<MessageL❔ How can I fix this I keep getting null value in my string variable❔ How do I go from interface to implementation while navigating ASP.NET core code?I saw this youtube video and he seamlessly navigates the source? How can I do this? Thanks.Console Application published as PublishSingleFile the window auto close for few devloper.I have published a .exe file with runtime framework included, and distributed to other developer for✅ Set Background color for one specific Row in GridI have a WPF project and a grid with a bunch of rows and columns. How can I change the background co❔ Facial recognition and centering using emgucv and arduino servos (visual studio)Hi guys, I am code illiterate. I found this code and need help because I do not know how to fix any ❔ Speech Recognizition makes me wanna cryI have already tried, using speech recogniztion on C++,Rust and now C#, since when this was that har