开发者

How can I override the ToString() method to output a list?

Is it possible to override the ToString() method to output a list? Here's my code

    namespace CircularClass
{
    class InfinitelyCircular
    {

        public class RatsInAShip
        {
            public string RatBreed;
            public string RatWeight;
            public string RatAge;
            public bool HasChildren;
            public List<RatsInAShip> RatChildren;
            public override string ToString()
            {
                return String.Format("RatBreed:{0}\n RatWeight:{1}\n RatAge:{2}\n HasChildren:{3}",RatBreed, RatWeight, RatAge, HasChildren);
            }
        }

        static void Main(string[] args)
        {
            var mu = new RatsInAShip()
                         {
                             RatAge = "100",
                             RatBreed = "RexRat",
                             RatWeig开发者_StackOverflow中文版ht = "200",
                             HasChildren = true,
                             RatChildren = new List<RatsInAShip>()
                         };

            for (var i = 0; i < 3; i++)
            {
                var cu = new RatsInAShip()
                             {
                                 RatAge = "100",
                                 RatBreed = "DwarfRat"+i,
                                 RatWeight = "200",
                                 HasChildren = false
                             };
                mu.RatChildren.Add(cu);
            }

            //Output
                Console.WriteLine(String.Format("{0}, {1}", mu.RatBreed, mu.RatChildren[0]));

        }
    }
}

How can I override ToString() to output all the children in a list?


Looks like you're already overriding the ToString() method on your class... so... use a StringBuilder and a for/foreach loop to construct your output string?


Perhaps you would could output the children in a foreach loop, like this:

public override string ToString()
{
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("RatBreed:{0}\n RatWeight:{1}\n RatAge:{2}\n HasChildren:{3}\n",RatBreed, RatWeight, RatAge, HasChildren);
    foreach (RatsInAShip rat in RatChildren)
    {
        sb.AppendFormat("\tRat Child: {0}\n", rat);
    }
    return sb.ToString();
}

I haven't compiled this, so please correct me if I made a mistake. [edit] compiled now... :)


To output a list? As in, return a list? No. Two reasons:

  1. You can't change the return type of an overridden method.
  2. ToString() implies that it returns a string. Returning anything else would be highly unintuitive and invites support problems with the code.


You can't change the return type of an overriden method.

You will need to write your own method that returns a list:

public IList<string> ToList()
{
  // your code that returns a list of strings
}


Yes:

public override string ToString()
{
    string result = "";
    if(RatChildren != null)
      foreach(var x in RatChildren)
        result += String.Format("RatBreed:{0}\n RatWeight:{1}\n RatAge:{2}\n HasChildren:{3}", x.RatBreed, x.RatWeight, x.RatAge, x.HasChildren);

    return result;
}


You can override the ToString as you have done already. You could iterate through the elements of the list to output the string of your desire.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜