C#
C#

help

Root Question Message

✞Tony
✞Tony2/10/2023
✅ Tuples in classes

using System.Collections.Specialized;

class Program
{
    static void Main(string[] args)
    {
        string[] input = Console.ReadLine().Split(' ');
        Player player = new Player(input[0], Int32.Parse(input[1]));
    }
    class Player
    {
        public string _playerName;
        public int playerID;
        public Player(string playerName,int playerIdentifier)
        {
            _playerName = playerName;
            playerID = playerIdentifier;
            Tuple<string, int> playerInfo = new Tuple<string, int>(_playerName,playerID);
        }
    }
}

This may be a beginner question but how do i acces the playerInfo for each Player instance created?
✞Tony
✞Tony2/10/2023
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
Playboi17
Playboi172/10/2023
There’s a lot of better options here than using a tuple.
Playboi17
Playboi172/10/2023
What would you be using to access the player?
Playboi17
Playboi172/10/2023
It’s name, id, what?
✞Tony
✞Tony2/10/2023
is not for a game is just for me learning c# and yes his name and his id
✞Tony
✞Tony2/10/2023
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
Playboi17
Playboi172/10/2023
All you need is a List<Player> Players. Then do Players.FirstOrDefault(player => player.id == id)
Playboi17
Playboi172/10/2023
I’m on mobile
✞Tony
✞Tony2/10/2023
Uh i don't think i know what that is
✞Tony
✞Tony2/10/2023
and how to use it
Playboi17
Playboi172/10/2023
It’s C#s query language
Playboi17
Playboi172/10/2023
It’s basically saying return the first player in this list where the players id matches my provided id
Playboi17
Playboi172/10/2023
If no player in the list matches the id, return default (null)
✞Tony
✞Tony2/10/2023
so when i do List<Player> Players; and i iterate trough all of them i will get all the instances?
Playboi17
Playboi172/10/2023
Yes
✞Tony
✞Tony2/10/2023
Damn c# is nice
Playboi17
Playboi172/10/2023
If you put all the players in the list
Playboi17
Playboi172/10/2023
It is very nice
✞Tony
✞Tony2/10/2023
thanks man
Playboi17
Playboi172/10/2023
No problem
thinker227
thinker2272/10/2023
Btw, you should always use (string, int) over Tuple<string, int>
✞Tony
✞Tony2/10/2023
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
thinker227
thinker2272/10/2023
(string, int) is a shorthand for ValueTuple<string, int>, which is a better and more lightweight option to Tuple<string, int>
✞Tony
✞Tony2/10/2023
thanks i will keep that in mind
thinker227
thinker2272/10/2023
Iterate over a tuple?
✞Tony
✞Tony2/10/2023
no
✞Tony
✞Tony2/10/2023
    class Player
    {
        public string _playerName;
        public int playerID;
        public Player(string playerName,int playerIdentifier)
        {
            _playerName = playerName;
            playerID = playerIdentifier;
            List<Player> Players;
        }
    }
✞Tony
✞Tony2/10/2023
how do i iterate over Players
thinker227
thinker2272/10/2023
foreach (Player player in Players) { ... }
✞Tony
✞Tony2/10/2023
oh ok yeah i was using foreach (Player in Players) { ... }
thinker227
thinker2272/10/2023
you have to specify the type, yeah
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy