Wednesday, May 6, 2015

C# Debug-Info

Hi,

a few days ago I found a quite cool feature:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestProject
{
    class Program
    {
        [DebuggerDisplay("Person: '{Name}'")]
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
        static void Main(string[] args)
        {
            var persons = new List<Person>(new Person[] { 
                new Person() { Name = "Johnny Galecki", Age = 40},
                new Person() { Name = "Kaley Cuoco-Sweeting", Age = 30},
            });

            Debugger.Break();

            if (Debugger.IsLogging())
            {
                persons.ForEach(x => Debugger.Log(0, "output", x.Name + Environment.NewLine));
            }
        }
    }
}

In line 12 DebuggerDisplay tells the Debugger to show the information "person" and then the person's name. E.g.: if you are running this example application the debugger will break (like setting a break-point) on line 25. When you check the variable using a "watch"-window or "local" you will see that the output is now something helpful instead of the full qualified class name.

Other important features related to Debug-Info:
  • DebuggerStepThrough-Attribute
  • DebuggerBrowsable-Attribute
kind regards,
Daniel

EDIT: check also DebuggerTypeProxy: https://msdn.microsoft.com/en-us/library/d8eyd8zc.aspx 

No comments: