C#:如何获取我的卡片打印列表?

时间:2019-06-14 03:45:36

标签: c#

我正在尝试打印我的卡片列表,因此在下面我要说的只是返回ToString()的卡片列表版本:

  public override string ToString()
  {
      return Cards.ToString();
  }

因此,当我在卡片列表中调用ToString()时,它将返回该卡片列表的字符串表示形式,而我将从ToString()中返回它。所以我试图在这里建立代表团。

因此,每当我调用System.Console.WriteLine(deck);时,都应该调用ToString(),并且我想将打印委托给属于卡片列表的ToString()函数,如下所示:

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();
  }
}

当我尝试运行它时,我发现一遍又一遍的[Card]实例,但实际上打印出来的是System.Collections.Generic.List 1 [Card] {{ 1}} System.Collections.Generic.List one time. I don’t know why1打印出来。

所以我的猜测是我返回或几乎返回了该卡列表的表示,但是,当我在卡列表中调用[Card]时又在我的Card类上调用了ToString()很好,它从最高层ToString()创建了一个庞大的委托链,并调用了卡ToString(),并依次在ToString()类上调用了ToString()

所以我想我应该在Card类本身上定义一个ToString()函数,然后从那里返回卡的字符串表示形式,这就是我试图这样做的方式:

Card

但是那没什么发生,我希望看到印有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; } } 之类的东西,但是我继续得到[Ace of Diamonds, Two of Diamonds... 1 [Card]`

我试图按照以下文档寻求帮助: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8

但是我发现在C#的第一周有点不知所措。

我还尝试在此处遵循谜题: Why is my array of cards empty?

但是我仍然无法弄清楚。

3 个答案:

答案 0 :(得分:3)

您应该将ToString()类中的Deck方法更改为String.Join()结果:

public override string ToString()
{
    return "[" + string.Join(", ", Cards.Select(c => c.ToString())) + "]";
}

哪个给出以下结果:

[Ace of Diamonds, Two of Diamonds, Three of Diamonds, Four of Diamonds, Five of Diamonds, Ace of Hearts, Two of Hearts, Three of Hearts, Four of Hearts, Five of Hearts, Ace of Clubs, Two of Clubs, Three of Clubs, Four of Clubs, Five of Clubs, Ace of Spades, Two of Spades, Three of Spades, Four of Spades, Five of Spades]

从文档中可以看到,方法签名看起来类似于以下内容:

string Join (string separator, IEnumerable<string> values);

因此,我们需要传递一个字符串定界符和一个IEnumerable<string>集合。我们只给", "作为定界符,然后使用Enumerable.Select()

用LINQ从集合中的每个字符串中提取ToString()

答案 1 :(得分:1)

我是用Linq nm1 <- sub("\\d+$", "", names(df)) df[paste0(unique(nm1), "_mean")] <- sapply(split.default(df, nm1), rowMeans) string interpolation

完成的
Aggregate

答案 2 :(得分:0)

List.ToString()不会遍历列表中的所有元素,而是调用它们的toString()方法。你必须自己做。

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()
  {
      string s = "[";
      foreach (var card in Cards) {
        s += card.ToString() + ",";
      }
      s += "]";
      return s;
  }
}