Question regarding C#'s `List<>.ToString`

前端 未结 3 1425
滥情空心
滥情空心 2020-12-05 12:33

Why doesn\'t C# List<>\'s ToString method provide a sensible string representation that prints its contents? I get the class name (which I as

3条回答
  •  余生分开走
    2020-12-05 13:25

    I think the reason is, that it is unclear what it should actualy do.

    Maybe do ToString on ever elemenat and separate them with comas? But what if someone wants semicolons? Or dashes? Or someone wants to enclose whole string in curly or normal braclets? Or somone wants to use different function to get textual representation of single element?

    Few things to note: ToString should be used only for debuging purpouses. If you want to export your data into string, either override this behaviour in your class or make an utility class for it.

    Also List is intended to store elements, not to provide their textual representation.

提交回复
热议问题