【问题标题】:How to display all values in a dictionary when the value is a class type [duplicate]当值是类类型时如何显示字典中的所有值[重复]
【发布时间】:2020-09-25 02:23:37
【问题描述】:

我有一本字典,试图从中显示所有值。我找到的答案的问题是它们只返回键和值。我的值是我在存储库中创建的类型。

  1. 下面是我的字典。我的价值是具有多个属性的 Badge 类:
public class BadgesRepository
{
    private Dictionary<int, Badge> badgeDictionary = new Dictionary<int, Badge>();
    public void AddBadgeDictionaryEntry(int badgeID, Badge newBadge)
    {
        badgeDictionary.Add(badgeID, newBadge);
    }
}
  1. 下面是我的徽章类:
public class Badge
{
    public int BadgeID { get; set; }
    public string BadgeName { get; set; }
    public List<string> DoorList { get; set; }
}
  1. 这里我想显示字典的所有值:
private void ViewAllBadges()
{
    Console.WriteLine("All badge data is listed below: ");
    Dictionary<int, Badge>.ValueCollection values = 
    foreach (Badge val in values)
    {
        Console.WriteLine("Badge ID: {0}", val);
    }
}

【问题讨论】:

标签: c# dictionary


【解决方案1】:

你只需要一个用于 Badge 类的 ToString 方法,我想。 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method。 基于他们的例子:

class Person
{
    public int BadgeID { get; set; }
    public string BadgeName { get; set; }
    public List<string> DoorList { get; set; }

    public override string ToString()
    {
        return BadgeID.ToString() + " " + 
               BadgeName + " "+
               String.Join(", ", DoorList.ToArray()); 
    }
}

【讨论】:

    【解决方案2】:

    您可以在 Badge 类中覆盖 ToString(),就像 roadrunner66 提到的那样

    然后

    foreach (var item in badgeDictionary)
    {
        Badge badge = item.Value;
        Console.WriteLine(badge.ToString());
    }
    

    或者

    foreach (var item in badgeDictionary)
    {
        Badge badge = item.Value;
    
        Console.WriteLine("Badge ID: {0} BadgeName {1} DoorList {2}", badge.BadgeID, badge.BadgeName, String.Join(", ", badge.DoorList.ToArray()));
    }
    

    (上面的代码只是想法,我还没有尝试构建这个代码,所以可能会出错。)

    【讨论】:

      【解决方案3】:
      private void ViewAllBadges()
      {
         Console.WriteLine("All badge data is listed below: ");
         Dictionary<int, Badge>.ValueCollection values = 
         foreach (Badge val in values)
         {
             Console.WriteLine("Badge ID: {0}", val);
         }
      }
      


      您似乎想使用 Dictionary&lt;TKey,TValue&gt;.Values 属性。


      ViewAllBadges 方法添加到您的BadgesRepository 类中。注意foreach 循环中的.Values 属性:

      public class BadgesRepository
      {
          private Dictionary<int, Badge> badgeDictionary = new Dictionary<int, Badge>();
      
          public void AddBadgeDictionaryEntry(int badgeID, Badge newBadge)
          {
              badgeDictionary.Add(badgeID, newBadge);
          }
      
          public void ViewAllBadges()
          {
              foreach (Badge badge in badgeDictionary.Values)
              {
                  Console.WriteLine(
                      $"Badge ID: {badge.BadgeID}, " +
                      $"Badge name: {badge.BadgeName}, " +
                      $"Door list: {string.Join(", ", badge.DoorList)}");
              }
          }
      }
      

      您也可以按照其他人的建议在您的Badges 班级中override ToString()...

      public class Badge
      {
          public int BadgeID { get; set; }
          public string BadgeName { get; set; }
          public List<string> DoorList { get; set; }
      
          public override string ToString()
          {
              return $"Badge ID: {BadgeID}, " +
                      $"Badge name: {BadgeName}, " +
                      $"Door list: {string.Join(", ", DoorList)}";
          }
      }
      

      ...并简化ViewAllBadges() 方法:

      public void ViewAllBadges()
      {
          foreach (Badge badge in badgeDictionary.Values)
          {
              Console.WriteLine(badge.ToString());
          }
      }
      

      这是一个示例调用代码:

      var bd = new BadgesRepository();
      
      var badge1 = new Badge();
      badge1.BadgeID = 1;
      badge1.BadgeName = "One";
      badge1.DoorList = new List<string>() { "Door 1", "Door 100" };
      bd.AddBadgeDictionaryEntry(1, badge1);
      
      var badge2 = new Badge();
      badge2.BadgeID = 2;
      badge2.BadgeName = "Two";
      badge2.DoorList = new List<string>() { "Door 2", "Door 200" };
      bd.AddBadgeDictionaryEntry(2, badge2);
      
      var badge3= new Badge();
      badge3.BadgeID = 3;
      badge3.BadgeName = "Three";
      badge3.DoorList = new List<string>() { "Door 3", "Door 300" };
      bd.AddBadgeDictionaryEntry(3, badge3);
      
      bd.ViewAllBadges();
      

      【讨论】:

        猜你喜欢
        • 2023-02-09
        • 2021-04-04
        • 2021-06-21
        • 1970-01-01
        • 1970-01-01
        • 2013-01-23
        • 1970-01-01
        • 2018-12-04
        • 1970-01-01
        相关资源
        最近更新 更多