Click here to Skip to main content
13,630,702 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.6K views
5 bookmarked
Posted 4 Apr 2011
Licenced CPOL

Utilizing Debug class in .NET

, 4 Apr 2011
Rate this:
Please Sign up or sign in to vote.
Demonstrate the functionality provided by Debug class in .NET
One of the alternatives yet underutilized way to debug code in .NET is using Debug class found in System.Diagnostics Namespaces. This class really gives a bunch of functionality to developers that are very useful for debugging and I wonder why but I have observed that fewer percentage of developers use it. So I thought of sharing the usage of Debug class with developers out there as I found it very useful.

I will directly jump to the code snippet demonstrating the functionality of Debug class:

using System.Collections.Generic;
using System.Diagnostics;

namespace DemoDebugClass
{
    public class Customer
    {
        public int    Id   { get; set; }
        public string Name { get; set; }
        public int    Age  { get; set; }
    }

    public static class CustomerExtensions
    {
        public static void PrintCustomersBelowAge25(this IEnumerable<customer> customersList)
        {
            foreach (var customer in customersList)
            {
                Debug.WriteLine(string.Format("Customer Name: {0}", customer.Name));
                Debug.WriteLineIf(customer.Age < 25, "Required Customer Found!");
            }
        }
    }  
 
    class Program
    {
        static void Main(string[] args)
        {
            List<customer> customersList = new List<customer>();
            customersList.Add(new Customer { Id = 1, Age = 30, Name = "Customer A" });
            customersList.Add(new Customer { Id = 2, Age = 15, Name = "Customer B" });
            customersList.Add(new Customer { Id = 3, Age = 20, Name = "Customer C" });
            customersList.PrintCustomersBelowAge25();
        }
    }
}


In the above code snippet, we have a customer class and assume that we are interested in finding customers below age 25 as shown in the code snippet above. With the Debug.WriteLine, you can print data to Output Window in Visual Studio (View –> Output Window) and even conditional data as well, as depicted in the image below.

Output Window

Now this is really interesting because it gives liberty to developers to debug the program and data without using breakpoints as most of us used to do. Further, these lines are only executed when the program is executed in Debug mode. Besides, this also allows you to easily share the output log with your colleagues.

So go ahead and give it a try! If you don’t use it for some reason, then please share your remarks. Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Adil Mughal
Software Developer (Senior)
Australia Australia
Adil is an aspiring software craftsman. He is currently working as Senior Developer at Nintex and has extensive experience on designing and developing enterprise scale applications on Microsoft .NET Framework. Lately he is into cross platform mobile app development on Windows, Android and iOS.
Besides, day to day job, he is active in offline and online technical communities and often participates as speaker in technical events.
He blogs about his experience at http://www.AdilMughal.com

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 4 Thanks! I'm still learning WPF, C# ... Pin
Susan Heffron12-Apr-11 8:12
memberSusan Heffron12-Apr-11 8:12 
GeneralReason for my vote of 3 If you'd do some actual work in Prin... Pin
bigscout7912-Apr-11 4:09
memberbigscout7912-Apr-11 4:09 
GeneralReason for my vote of 2 debug with puting break point Pin
jigneshpatelit61198911-Apr-11 20:09
memberjigneshpatelit61198911-Apr-11 20:09 
GeneralReason for my vote of 5 nothing interesting Pin
Melionu4-Apr-11 21:12
memberMelionu4-Apr-11 21:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180712.1 | Last Updated 4 Apr 2011
Article Copyright 2011 by Adil Mughal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid