C#: Using the Debug class part 1

Sometimes you want to know what kind of data you are working with in your application. The standard solution for this is to add a breakpoint and hover it with your mouse when i hits:

Breakpoint debugging

There are easier ways though if you need to check large amounts on information. In the namespace System.Diagnostics there is a class called Debug which is very handy. It allows you to write information to the Output-window (can be opened in Visual Studio with View => Output or Ctrl + W, O).

Here’s an example of using the Debug class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace MaffeluDemo
    class Program
        static void Main(string[] args)
            IList<Person> people = new List<Person>()
                new Person(){Name = "Kalle", Age = 91},
                new Person(){Name = "Lisa", Age = 26},
                new Person(){Name = "Zlatan", Age = 29},
            //Direct user output
            Console.WriteLine("There are {0} people in the collection!", people.Count());
            foreach (Person person in people)
                Debug.WriteLine("Name: " + person.Name);
                Debug.WriteLineIf((person.Age > 65), "Senior citizen found!");
    class Person
        public string Name { get; set; }
        public int Age { get; set; }

If you run the following code you should see something like this:

This is what it should look like, check the Output window

If you want more information about the Debug class I’ve written a follow up on this article here: http://maffelu.net/c-using-the-debug-class-part/!

One thought on “C#: Using the Debug class part 1

Leave a Reply