RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 839653
Accepted
StriBog
StriBog
Asked:2020-06-09 03:35:48 +0000 UTC2020-06-09 03:35:48 +0000 UTC 2020-06-09 03:35:48 +0000 UTC

c# 为集合覆盖 ToString()

  • 772

如何覆盖集合的 ToString() 方法?

List<Place> spisok = new List<Place>();
for (int i = 0; i < 20; i++)
     spisok.Add(new Place(PlaceStatus.Free));

我想在打电话时spisok.ToString()收到格式为 0,0,0,0 的字符串(即用逗号分隔的元素)

或者类似的这样一个集合

var spisok = new Place[20];
for (int i = 0; i < 20; i++)
     spisok[i] = new Place(PlaceStatus.Free));

下面是使用的元素的代码。在类中我试图重载 ToString() 方法,当对象为 1 时,它会给出数值。但在数组的情况下,这是文本 - 关于类的记录

    public enum PlaceStatus
    {
        Free,
        Booked,
        Paid,
        Confirmed
    }
    public class Place
    {
        public PlaceStatus Status { get; set; }

        public Place(PlaceStatus status)
        {
            Status = status;
        }

        public override string ToString()
        {
            return ((int)Status).ToString();
        }
    }
c#
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Digital Core
    2020-06-09T15:14:24Z2020-06-09T15:14:24Z
    public class MyTypeList : List<MyClass>
    {
        public override string ToString()
        {
            return ...
        }
    }
    
    • 5
  2. Best Answer
    Kir_Antipov
    2020-06-09T03:48:37Z2020-06-09T03:48:37Z

    创建一个静态助手类。它看起来像这样:

    public static class PlaceCollectionHelper
    {
        public static string ToString(this IEnumerable<Place> Collection, string Separator)
        {
            return string.Join(Separator, Collection);
        }
    }
    

    然后在您的代码中使用:

    string spisokStr = spisok.ToString(",");
    

    UPD:
    以前的版本是一个扩展,因为您不能为现有集合显式覆盖 ToString()。但是,如果您需要第三方方法来调用您的集合到您需要的字符串的转换,您将必须创建一个新类型,该类型将从您需要的集合类型继承。这里有2种方法。让我们来看看它们。


    0)最简洁的解决方案将是相邻答案中提出的解决方案:

    public class PlaceCollection : List<Place>
    {
        public override string ToString()
        {
            return string.Join(", ", this);
        }
        public string ToString(string Separator)
        {
            return string.Join(Separator, this);
        }
    }
    

    您只需创建一个类,该类继承自您需要的类型的工作表,您可以在其中以您需要的方式覆盖 ToString()。如您所知,这种结构的工作方式与普通工作表相同:

    PlaceCollection collection = new PlaceCollection { new Place(PlaceStatus.Free), new Place(PlaceStatus.Booked) };
    collection.Add(new Place(PlaceStatus.Paid));
    

    但是,现在,如果你调用,比如说,Console.WriteLine当一个对象被传递给它时,它会自动调用 ToString() ,你会得到:

    Console.WriteLine(collection);
    // 0, 1, 2
    

    1) 仅当您将 List 用作集合时,上一个选项才适合您。但是,在您的第二个示例中,很明显您也在为自己的目的使用数组。因此,在解决您的问题时,从通用接口继承将帮助我们IEnumerable<T>:

    public class PlaceCollection : IEnumerable<Place>
    {
        #region Var
        private IEnumerable<Place> Collection { get; set; }
        #endregion
    
        #region Init
        public PlaceCollection(IEnumerable<Place> Collection)
        {
            this.Collection = Collection;
        }
    
        public static implicit operator PlaceCollection(List<Place> Collection)
        {
            return new PlaceCollection(Collection);
        }
        public static implicit operator PlaceCollection(Place[] Collection)
        {
            return new PlaceCollection(Collection);
        }
        #endregion
    
        #region Functions
        public IEnumerator<Place> GetEnumerator()
        {
            return Collection.GetEnumerator();
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return Collection.GetEnumerator();
        }
    
        public override string ToString()
        {
            return string.Join(", ", Collection);
        }
        public string ToString(string Separator)
        {
            return string.Join(Separator, Collection);
        }
        #endregion
    }
    

    可以使用 访问元素Linq,也可以添加自己的索引器。这个类实现了一个隐式转换,所以你可以这样做:

    Place[] placeArray = new Place[] { new Place(PlaceStatus.Free), new Place(PlaceStatus.Booked) };
    PlaceCollection collection = placeArray;
    

    或者像这样:

    List<Place> placeList = new List<Place> { new Place(PlaceStatus.Free), new Place(PlaceStatus.Booked) };
    PlaceCollection collection = placeList;
    

    但是,结果仍然是相同的:

    Console.WriteLine(collection);
    // 0, 1
    

    既然我们继承自IEnumerable<Place>,那么你可以得到一个枚举器:

    foreach (Place p in collection)
        Console.WriteLine(p);
    // 0
    // 1
    

    为您选择最方便的方法,祝您决策顺利)

    • 3

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5