I am trying to get my list of cards printing and so below I am saying let's just return the cards list version of ToString()
:
public override string ToString()
{
return Cards.ToString();
}
So when I call ToString()
on a list of cards, it will return a string representation of that list of cards and I am going to return that from my ToString()
. So I am trying to set up delegation here.
So anytime I call System.Console.WriteLine(deck);
it should call my ToString()
and I want to delegate printing to the ToString()
function that belongs to the list of cards like so:
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
Deck deck = new Deck();
System.Console.WriteLine(deck);
}
}
public class Deck {
public List<Card> Cards = new List<Card>();
public Deck() {
string[] ranks = { "Ace", "Two", "Three", "Four", "Five" };
string[] suits = { "Diamonds", "Hearts", "Clubs", "Spades" };
foreach (string suit in suits) {
foreach (string rank in ranks) {
Card card = new Card(rank, suit);
Cards.Add(card);
}
}
}
public override string ToString()
{
return Cards.ToString();
}
}
When I try running it, I see what I thought would be a whole bunch of instances of [Card]
over and over again, but what actually got printed out was System.Collections.Generic.List
1[Card]one time. I don’t know why
System.Collections.Generic.List1
printed out with [Card]
.
So my guess is that I returned or almost returned a representation of that list of cards, however, when I called ToString()
on the list of cards that in turn called ToString()
on my Card class as well which creates a huge chain of delegation from the very top level ToString()
gets called and that calls the card ToString()
and that in turn calls ToString()
on the Card
class as well.
So I thought I would define a ToString()
function on the Card
class itself and from there I can return a string representation of the cards and that's what I attempted to do like so:
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
Deck deck = new Deck();
System.Console.WriteLine(deck);
}
}
public class Deck {
public List<Card> Cards = new List<Card>();
public Deck() {
string[] ranks = { "Ace", "Two", "Three", "Four", "Five" };
string[] suits = { "Diamonds", "Hearts", "Clubs", "Spades" };
foreach (string suit in suits) {
foreach (string rank in ranks) {
Card card = new Card(rank, suit);
Cards.Add(card);
}
}
}
public override string ToString()
{
return Cards.ToString();
}
}
public class Card {
// properties
public string suit { get; set; }
public string rank { get; set; }
public override string ToString()
{
return $"{rank} of {suit}";
}
public Card(string rank, string suit){
//initializations
this.rank = rank;
this.suit = suit;
}
}
But that's not what happens, I expected to see printed up something like [Ace of Diamonds, Two of Diamonds...
and so on, but instead I continue to get System.Collections.Generic.List
1[Card]`
I attempted to follow this documentation for assistance: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8
but I found it to be a bit overwhelming on my first week of C#.
I also tried to follow Enigmativity in this question but I am still unable to figure this out.