❔ Check if list contains object with a specific property/variable value
I have a list of Players where Player is a simple object with a Name and ID. I need to check to see if a player with a specific ID is on the player list. I know that I could use a dictionary to do this but if there is a way to keep the list, that would be ideal. Here's a rough outline of what I'm trying to achieve
8 Replies
I also know that a foreach loop would achieve the desired result but it's long and ugly and if there's something simpler great. Otherwise I'll go with the foreach
if you are sure that player exists then
I mostly do this
Which utilists nullable reference types so it does not throw
obviously method should not be void for this case
Alternatively if you want something slightly more efficient, a regular for loop would probably be the best
Plz /close
you could use a SortedList<ushort, string> and/or SortedList<string, ushort>
Or a dictionary, I sometimes do that
depends on performance and memory occupation
for a small collection and fast creation/deallocation even a List<(string, ushort)>() could do
have you tried writing only /close
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.