I am writing code for a program that players poker. My question is concerning reference and value passing in Lists.
My program has a Player object, a Dealer object, and a PokerTable object. The PokerTable has two Lists of players, one called players and one called winners. The list of players is passed to the Dealer object’s DetermineWinner method which passes back a list with the winner (or winners in the case of a tie). The PokerTable then uses the list of winners to determine who to pay money to.
class PokerTable
{
private Dealer dealer;
private List<Player> players;
private List<Player> winners;
private void EndHand()
{
winners = dealer.DetermineWinner(players);
PayPlayers();
winners.Clear();
PrepareForNextHand();
}
private void PayPlayers()
{
/* code here uses the winners list to determine
* how much to pay each winner and adds that money
* to the player object */
}
My question is whether adding money to the player objects in the winners list will also effect the original player objects in the original players list.
List<Player>is a list of references toPlayerobjects.If a reference in two separate lists points to the same
Playerthen updating aPlayerin one list will appear to have changed thatPlayerin the other list as well.On the other hand, if your list points to a value type then of course the changes are not reflected: