C
C#2y ago
Tony

✅ 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);
}
}
}
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?
22 Replies
Tony
TonyOP2y ago
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
Connor
Connor2y ago
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?
Tony
TonyOP2y ago
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
Connor
Connor2y ago
All you need is a List<Player> Players. Then do Players.FirstOrDefault(player => player.id == id) I’m on mobile
Tony
TonyOP2y ago
Uh i don't think i know what that is and how to use it
Connor
Connor2y ago
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)
Tony
TonyOP2y ago
so when i do List<Player> Players; and i iterate trough all of them i will get all the instances?
Connor
Connor2y ago
Yes
Tony
TonyOP2y ago
Damn c# is nice
Connor
Connor2y ago
If you put all the players in the list It is very nice
Tony
TonyOP2y ago
thanks man
Connor
Connor2y ago
No problem
Thinker
Thinker2y ago
Btw, you should always use (string, int) over Tuple<string, int>
Tony
TonyOP2y ago
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
Thinker
Thinker2y ago
(string, int) is a shorthand for ValueTuple<string, int>, which is a better and more lightweight option to Tuple<string, int>
Tony
TonyOP2y ago
thanks i will keep that in mind
Thinker
Thinker2y ago
Iterate over a tuple?
Tony
TonyOP2y ago
no
class Player
{
public string _playerName;
public int playerID;
public Player(string playerName,int playerIdentifier)
{
_playerName = playerName;
playerID = playerIdentifier;
List<Player> Players;
}
}
class Player
{
public string _playerName;
public int playerID;
public Player(string playerName,int playerIdentifier)
{
_playerName = playerName;
playerID = playerIdentifier;
List<Player> Players;
}
}
how do i iterate over Players
Thinker
Thinker2y ago
foreach (Player player in Players) { ... }
Tony
TonyOP2y ago
oh ok yeah i was using foreach (Player in Players) { ... }
Thinker
Thinker2y ago
you have to specify the type, yeah
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server