Thursday 27 July 2017

DebuggerDisplay Attribute

While working on complex classes/objects, it really takes two more clicks to either add that object to the Watch window or expand the object in visualizer and navigate to specific property.
To avoid these extra steps and to make your debugging bit faster, you can decorate class with specific property using DebuggerDisplay attribute. Applying this attribute makes sure that when you add object to Watch window, it displays the value of above property in Value column.

For example, I have below Model class for which I am creating an object and list of objects with some dummy values. Also, I have decorated this class with DebuggerDisplay attribute that has definition as to display name of the person, ID of the person and length of a name.

/// <summary>
/// Debugger Display Attribute – Model class
/// </summary>
[DebuggerDisplay("Emp ID = {name} - {ID} - {name.Length}")]
public class TestDebuggerDisplay
{
    public string name
    {
      get; set;
    }
    public int ID
    {
      get; set;
    }
}

Main Class:

class Program
{
    static void Main(string[] args)
    {

        TestDebuggerDisplay obj = new TestDebuggerDisplay() { ID=1, name="name1" };
        List<TestDebuggerDisplay> lstObjs = new List<TestDebuggerDisplay>();

        for (int i = 0; i < 5; i++)
        {
        lstObjs.Add(new TestDebuggerDisplay()
                {
                ID = 10 * (i + 1),
                name = $"name{10 * (i + 1)}"
                }
                );
         }

            Console.WriteLine();
   }
}

Output:

While you are debugging, you would see that, when the object is added to watch window you can see the value for that object as below:







Sunday 23 July 2017

DebuggerStepThrough Attribute

While working on a complex code/ software, we often come across situations where you need to debug lots of code to find/fix the issue. That also includes going through lots of methods (probably related or unrelated). .NetFramework provides way to avoid unwanted "Step in" through methods. Using DebuggerStepThrough (using System.Diagnostics) attribute, you can skip debugging of unwanted methods and reduce the debugging time to some what extent.
For example, I have below class file that contains two classes ( a class in which there is a method to reverse a string and a class that logs reversed string to output window.).

If DebuggerStepThrough attribute is applied to method, any breakpoints inside that method won't be hit while debuggin the application.

If DebuggerStepThrough attribute is applied to class, any breakpoints inside the class will not be hit.
 
class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("test");
            string name = "TestString";
            name = ReverseString(name);
            Console.WriteLine(name);
            Logger.LogMessages(name);
 
        }
 
        private static string ReverseString(string name)
        {
            Debug.WriteLine(name);
            Debug.WriteLine(name.Length);
            char[] strArray = name.ToCharArray();
            Array.Reverse(strArray);
 
            return new string(strArray);
        }
    }
Second class:
public class Logger
   {
       [DebuggerStepThrough]
       public static void LogMessages(string data)
       {
           if (data==null)
           {
               Console.WriteLine("Null Variable");
           }
           else
           {
               Debug.WriteLine(data);
           }
 
       }
   }
Output: