2

In C# .Net we see that System.Diagnostics.DebuggerDisplayAttribute can display customized information during a debugging session. This is useful, and easy to display single values.

But what about arrays? Take the below snippet as an example. I am constantly switching between the two attributes by commenting/uncommenting because I have a usage scenario where MQueue is 5 elements long, and another where it is 2. Is there a way for the DebuggerDisplay attribute to handle arrays so that I don't have to hard-code the display statements?

    //[DebuggerDisplay("[{MQueue[0]} {MQueue[1]} {MQueue[2]} {MQueue[3]} {MQueue[4]}]")]
    //[DebuggerDisplay("[{MQueue[0]} {MQueue[1]}]")]
    internal class State
    {
        internal List<int> MQueue { get; set; }

    }
sapbucket
  • 6,795
  • 15
  • 57
  • 94
  • I have not tested but `[DebuggerDisplay("MQueue[0]:{MQueue[0]}, MQueue[1]:{MQueue[1]}, MQueue[2]:{MQueue.Count()>=3?MQueue[2]:(int?)null}, ...")` should do the trick. I have the code from [here](https://www.selfelected.com/tip-when-using-debuggerdisplay-in-dotnetvisual-studio/) – LosManos Aug 15 '16 at 14:06

1 Answers1

1

My apologies, this is a duplicate of: How to make [DebuggerDisplay] respect inherited classes or at least work with collections?

The specific answer to my question is therefore:

[DebuggerTypeProxy(typeof (StateDebugView))]
internal class State
{
    internal List<int> MQueue { get; private set; }

}

internal sealed class StateDebugView
{
    private readonly State _sealedState;

    public StateDebugView(State sealedState)
    {
        _sealedState = sealedState;
    }

    [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
    public int[] Items
    {
        get { return _sealedState.MQueue.ToArray(); }
    }
}
Community
  • 1
  • 1
sapbucket
  • 6,795
  • 15
  • 57
  • 94
  • moderator, I do believe that my post is more specific and better than the duplicate that I quoted above. I believe that if someone else wanted to find an answer to the question that I posted that my answer is more appropriate that the duplicate OP. – sapbucket Jul 22 '15 at 16:10