0

I want to have a class derived from List<T>, mainly for the purpose to be able to be shown different stuff than the usual Count = 3 in the debugger of VisualStudio 2012. I thought this would be easy by providing my own overriden ToString() method for my own overriden generic MyList<T> class.

It doesn't work though, the overriden ToString() is not called from the debugger and I still get to see the old Count = 3.

Here is the code.

using System; 
using System.Collections.Generic;

namespace ConsoleApplication1 {
    public class MyList<T> : List<T>
    {
        public override string ToString()
        {
            int nMax = 100;
            string s = "N:" + this.Count + "; ";
            for (int i = 0; i < this.Count && s.Length < nMax; ++i)
                s += this[i].ToString() + "; ";
            return s;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            MyList<string> a = new MyList<string> { "a", "b", "c" };
            Console.WriteLine(a.ToString());
        }
    } 
}

When setting a breakpoint onto the last brace of the Main method, I get to see in the debugger Auto- or Local-window this: a | Count = 3 | ConsoleApplication1.MyList<string>

I want and expected to be shown there: a | N:3; a; b; c; | ConsoleApplication1.MyList<string>

In the console window it is written N:3; a; b; c;, which shows that the method and the overriding in principle do work. Also, calling a.ToString() in the Direct-Window shows the expected N:3; a; b; c;.

What am I missing? How can I solve my problem?

AndreasW
  • 83
  • 7

0 Answers0