Julien Julien - 1 month ago 7
C# Question

C# - Field values of an array picking up values of a loop

I'm training C# on a simple card game. I have methods that shuffle and deals the cards. I have a random deck that is well generated.
Is it possible to set an array for the player1 cards, picking up the first ten values of the array.
Here is a part of my code :

currentCard = 0;
public Card DealCard()
{
if (currentCard < deck.Length)
return deck[currentCard++];
else
return null;
}


I want to pick up for example ten first values of

deck[currentCard++]


Any suggestions will be appreciated, thanks for your help !

Answer Source

You mean you want to pull the first 10 enties into another array? Something like;

var player1Cards = deck.Take(10);

or

List<int> player1Cards = new List<int>();
for (int i = 0; i < 10; i++){
    player1Cards.Add(deck[i]);
}