简体   繁体   English

替代清单 <T> .ToString()

[英]override list<T>.ToString()

ok, im in learning stage already, make search and dont find, i want return .ToString() from my list<> but this in using 好的,我已经处于学习阶段,进行搜索并找不到,我想从我的列表中返回.ToString()<>,但这在使用中

using System.Collections.Generic; 
using System.Text;

namespace eciv {
    using Pontos = List<Ponto>;  // I need override ToString()

    public partial class FPrincipal : Form  {

        private void button1_Click(object sender, EventArgs e)  {
            Pontos aa = new Pontos();
            aa.Add(new Ponto(2, 3));
            aa.Add(new Ponto(3, 4));
            textBox1.Text = aa.ToString();  // here I need help  
        }
    }

    public class Ponto {

        public double X;
        public double Y;

        public Ponto() {}

        public Ponto(double x, double y) { 
            this.X = x; this.Y = y; 
        }

        public Ponto(Ponto p) { 
            this.X = p.X; 
            this.Y = p.Y; 
        }

        public override string ToString() {
            string s = "(" + this.X.ToString() + "," + this.Y.ToString() + ")";
            return s;
        }
    }   
}

In textBox I get this value 在文本框中,我得到这个值

System.Collections.Generic.List`1[eciv.Ponto]

I want something like this 我想要这样的东西

((2,3),(3,4))

I think it's this but it doesn't work 我认为是这样,但是不起作用

public override string ToString() : Pontos {  
    StringBuilder s = new StringBuilder();
    s.Append("(");

    foreach (Ponto p in this) 
    {  
        s.Append(p.ToString() + ",");
    }

    s.Replace(",", ")", s.Length - 1, 1);
    return s.ToString();
}

You cannot override a method in some class if you are not inheriting from that class. 如果您不从某个类继承方法,则不能覆盖该类中的方法。 So the solution is simple - instead of creating name alias, create a class which inherits List<Ponto> : 因此解决方案很简单-创建一个继承List<Ponto>的类,而不是创建名称别名:

public class Pontos : List<Ponto>
{
    public override string ToString() => $"({String.Join(",", this)})";
}

Or format list in place: 或格式化清单:

textBox1.Text = $"({String.Join(",", aa)})"; // consider use meaningful names for variables

I would go for the second option if you want to convert pontos list to string only once - string formatting is not a good reason to introduce a new type. 如果您只想将浮桥列表转换为字符串一次,我会选择第二种方法-字符串格式并不是引入新类型的好理由。


If you need to convert this list to string several times, you can create some private or extension method for formatting list of objects (not necessary Ponto objects). 如果需要多次将此列表转换为字符串,则可以创建一些私有或扩展方法来格式化对象列表(不必要的Ponto对象)。 Eg 例如

public static string Stringify<T>(this IEnumerable<T> values)
   => $"({String.Join(",", values})";

And usage 和用法

textBox1.Text = aa.Stringify(); // works with List<int> etc

Links: 链接:

You can create your own List<T> , which overrides the ToString() -method 您可以创建自己的List<T> ,它重写ToString() -方法

public class MyList<T> : List<T>
{
    public override string ToString()
    {
        StringBuilder s = new StringBuilder();
        s.Append("(");
        foreach (var element in this)
        {
            s.Append(element.ToString() + ",");
        }
        s.Replace(",", ")", s.Length - 1, 1);
        return s.ToString();
    }
}

Instead of calling ToString() on List<T> , you can create a string based on ToString() implementation you create for Ponto class like this: 无需在List<T>上调用ToString() ,而是可以基于为Ponto类创建的ToString()实现创建一个字符串,如下所示:

Pontos aa = new Pontos();
aa.Add(new Ponto(2, 3));
aa.Add(new Ponto(3, 4));

StringBuilder s = new StringBuilder();
s.Append("(");
s.Append(String.Join(",", aa));
s.Append(")");

textBox1.Text = s.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM