✅ Tuples in classes
This may be a beginner question but how do i acces the playerInfo for each Player instance created?
22 Replies
how can i put that tuple into a list so whenever i need a player name and his id i can get it from that list
There’s a lot of better options here than using a tuple.
What would you be using to access the player?
It’s name, id, what?
is not for a game is just for me learning c# and yes his name and his id
i want both of them stored somewhere and when i check all the instances of Player Class i can see all of them grouped name with id
All you need is a List<Player> Players. Then do Players.FirstOrDefault(player => player.id == id)
I’m on mobile
Uh i don't think i know what that is
and how to use it
It’s C#s query language
It’s basically saying return the first player in this list where the players id matches my provided id
If no player in the list matches the id, return default (null)
so when i do List<Player> Players; and i iterate trough all of them i will get all the instances?
Yes
Damn c# is nice
If you put all the players in the list
It is very nice
thanks man
No problem
Btw, you should always use
(string, int)
over Tuple<string, int>
uh but if i want to iterate over them with a foreach() how do i use that like with a foreach(string player in Players) or how
(string, int)
is a shorthand for ValueTuple<string, int>
, which is a better and more lightweight option to Tuple<string, int>
thanks i will keep that in mind
Iterate over a tuple?
no
how do i iterate over Players
foreach (Player player in Players) { ... }
oh ok yeah i was using foreach (Player in Players) { ... }
you have to specify the type, yeah
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.