Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

C# Debugger Attributes Explained

1. Introduction to Debugger Attributes

In the last example, we saw about Logging and getting function call stack data. In this article, we will see about Debugger Attributes which controls debugging behavior of the objects and gives a rich experience to the debugging user. An Attribute is a Tag set over the elements like Class, Functions, assemblies, etc. These tags work out how the elements should act at run time. Let us see below specified debugging attributes with a simple example:

  1. DebuggerBrowsable Attribute
  2. DebuggerDisplay Attribute
  3. DebuggerHidden Attribute

2. About The Example

The below screenshot shows the example we are going to create here:

Debugging Attributes C# Example

Debugging Attributes C# Example

In each button click handler, we invoke the debugger break-point at run-time and hence we tell you to start the example through Visual Studio with F5 (i.e.) Start the sample through the menu option Debug->Start Debugging. Once you download this example, watch the video, which points out the usage of each attribute. In each button click, we will try one debugger attribute and how the debugging object acts.

3. The Book Class

First a class called

Book

is defined to examine the debugging attributes. This class has three private members and a constructor to initialize those private members. The code for the class is below:

4. Default Behavior Of Class While Debugging

The Default Class button click handler checks the behavior of the above book class. In the form file, to break into the code dynamically for debugging, we use the

System.Diagnostics

namespace. The Code is below:

The Default Class button click handler creates the instance of Book to examine the default behavior. The code is below:

When we debug the class instance

bk

, we can see all the class member information in the debugger window. Also, the debugger shows the Namespace and Class Name in the instance level. Have a look at the below video to know the default behaviour of the Book Class Instance:


Video 1: Default Book Class Behavior


5. Book Class Behavior in Debugger With ToString Override

Once we override the

ToString()

Method in the class, the debugger knows how to translate the class in the string format. During the debug time, the debugger will invoke the

ToString

 implementation provided by us. Have a look at the below code:

In the above code, we form a string based on the members present in the class and that string is returned to the caller.  In the debugger output, we can now see the user friendly data against the class Instance Name instead of the default Namespace.Classname. We already saw the default in the previous video under the type column of debugger window.

6. DebuggerBrowsable Attribute

There are various Debugging Windows like Auto, Quick-Watch etc can browse the class and read the values in each member. The

DebuggerBrowsable

  Attribute controls what member data can be browse-able through the debugger. To test the

DebuggerBrowsable

 Attribute, we change the default Book class in the early section, and we keep the revised version of the book class as Book1. Have a look at the Book1 class below:

In the above code, notice how we mark the member

m_publisher

with

DebuggerBrowsable

Attribute. Here, we set

DebuggerBrowsableState.Never

  through the attribute. The Never state tells the runtime that the marked member should not be browse-able while debugging any object of the Book1 class. There are other browse-able states and we can list those as below:

  1. Collapsed – Shows the element as collapsed.
  2. Never – Never show the element.
  3. RootHidden – Do not display the root element; display the child elements if the element is a collection or array of items.

In the Main form, we add the code in the Browsable button click handler to test the class Book1. The code is below:


Video 2: DebuggerBrowsable Attribute and effect of ToString Override


7. DebuggerDisplay Attribute

We can mark the debugger display attribute for elements like class, functions, properties etc. Look at the below screen-shot:

DebuggerDisplay Attribute

DebuggerDisplay Attribute

Here, we define the

<strong>DebuggerDisplay</strong> 

Attribute for the class member

m_author

.  The attribute is marked as 1 in the above picture, and the attribute takes a string. Note, the Debugger display string is accessing the class member within curly basis (Marked as 2). At runtime, C# will substitute the value from the actual variable

m_author

(marked as 3). To examine the above attribute, we change the basic Book class to have Book2 class. In this class, we mark all three data members with

DebuggerDisplay

attribute. The code is below:

In the Main form, we handle the click event for the Debugger Display button. The event handler code is listed below:


Video 3: DebuggerDisplay Attribute in action


8. DebuggerHidden Attribute

When you mark a member function with

DebuggerHidden

attribute, the debugger will not stop on that method! Means, you can’t keep a breakpoint on that method. To check this attribute, we change the Book2 class to have a new method called

GetPrice

which returns different price based on the month-Range in a year. Since we mark the

GetPrice

with

DebuggerHidden

 Attribute, we can’t debug this function in the Book2 class. The code is below:

The click event handler for the Hide Function button creates the Instance of Book2 and makes a call to

GetPrice

. You can examine the function to see how debugging is prohibited for the

GetPrice

. Below is code for button click event handler:


Video 4: Checking the DebuggerHidden Attribute


Source Code: Download Debugger Attributes C# Example From Google Drive

Categories: C#

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.