0% found this document useful (0 votes)
86 views

Debugging Tips For Csharp

The document provides several tips for debugging C# code, including placing breakpoints, overriding the ToString method, and using the System.Diagnostics namespace. Overriding ToString allows objects to be more readable when examined in debug mode. The Debug class within System.Diagnostics allows writing messages and conditional messages to the output window, helping with debugging loops and conditions.

Uploaded by

Justin Robinson
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Debugging Tips For Csharp

The document provides several tips for debugging C# code, including placing breakpoints, overriding the ToString method, and using the System.Diagnostics namespace. Overriding ToString allows objects to be more readable when examined in debug mode. The Debug class within System.Diagnostics allows writing messages and conditional messages to the output window, helping with debugging loops and conditions.

Uploaded by

Justin Robinson
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DEBUGGING TIPS FOR C# Debugging in C#, although much easier than in, say, PHP or C, can be a &!#@!

~ and can take a lot of time. The most common way to find an error is to place a breakpoint where you think the error might arise and then follow the execution from there.

Overriding ToString
Starting with this simple technique, there is a way to improve even this, namely by overriding an objects ToString method. If we take a simple application like this one:
01 static void Main(string[] args) 02 { 03 04 05 06 07 08 09 10 11 12 13 14 } 15 16 class Person 17 { 18 19 20 } public string Name { get; set; } public int Age { get; set; } foreach (Person p in people) { //Do something } }; IList<Person> people = new List<Person>() { new Person(){Name = "Kalle", Age = 91}, new Person(){Name = "Lisa", Age = 26}, new Person(){Name = "Zlatan", Age = 29},

If we run this code an place a breakpoint by the foreach loop to examine the 'people' collection, we will see something like this:

This means we have to enter each individual Person object to make sure we got what we wanted. An easy way to speed up this kind of debugging process is to override the Person classs ToString method:
01 class Person 02 { 03 04 05 06 07 08 09 10 11 } public override string ToString() { return string.Format("Name: {0}, Age: {1}", Name, Age); //return base.ToString(); } public string Name { get; set; } public int Age { get; set; }

If you debug now and hover the people collection then you will see this:

That's much better

Using System.Diagnostics
Another way which can be extremely usefull but which is much less common knowledge is using the System.Diagnostics namespace and the Debug class. With the debug class you can write data to the Output window in Visual Studio (found in the View menu, or by pressing Ctrl + W,O). You can do several interesting things with the Debug class. You may simply output data using the WriteLine() method, but you can also write using conditions, with the WriteLineIf() method which is even more outstanding. In the example below we are outputting all our person objects to the Output window, and if we find a senior citizen, we output that too: Here's an example:

01 class Program 02 { 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 } 21 22 class Person 23 { 24 25 26 27 28 29 30 31 32 } } public override string ToString() { return string.Format("Name: {0}, Age: {1}", Name, Age); //return base.ToString(); public string Name { get; set; } public int Age { get; set; } } Console.Read(); foreach (Person person in people) { Debug.WriteLine("Name: " + person.Name); Debug.WriteLineIf((person.Age > 65), "Senior citizen found!"); } }; 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},

And in the output window, you should see something like this:

Resized to 95% (was 531 x 252) - Click image to enlarge

Replies To: Debugging tips for C#


#2 chrixko
Hi these are nice tips. But there is an another elegant way to show relevant attributes of an object in debug-mode by using the DebuggerDisplayAttribute. With this attribute you can do something like:
1 [DebuggerDisplay("Name = {Name}, Age = {Age}, Street = {AddressAttributes[0]}")] 2 public class Person

If you run this in Debug-Mode the specified attributes are shown if you look up the value of a person object. There are even some more debugger attributes: http://msdn.microsof...y/ms228992.aspx http://msdn.microsoft.com/en-us/library/ms228992.aspx

#3 Sergio Tapia
Oh you glorious bastard. You have no idea how many hours you've saved by sharing that ToString override. So incredibly useful! How did I not know about it?

#4 DivideByZero
Great tips, especially the writelineif() method

#5 tlhIn`toq
Sergio Tapia, on 12 February 2011 - 06:13 AM, said: Oh you glorious bastard. You have no idea how many hours you've saved by sharing that ToString override. So incredibly useful! How did I not know about it?

I'd bet 70% of my classes override ToString(). How else do you have complex classes like Employee or ProductLineItem, etc. give nice looking results in ListBoxes and ComboBoxes etc.? DivideByZero, on 12 February 2011 - 06:15 AM, said: Great tips, especially the writelineif() method Good tutorial. Every debugging tutorial is a good thing. I'll be adding this to my list of tutorials for newbies. +1

#6 SwiftStriker00
Also in VS 2010 you can set conditional break points and hit count break points. These tools can be useful in loops especially if you dont want have to step through every iteration You can read more on the MSDN Page: http://blogs.msdn.com/b/zainnab/archive/2010/05/03/set-a-simple-breakpoint-condition-vstipdebug0021.aspx a word to the wise: conditional break points wont throw compiler errors, only runtime. Also make sure you do == and not = because it will actually set the variable and never break. The hit count break point is great in a loop because if your looping 1000 times, and your concerned with the 700th you dont want to hit continue 700 times. This break point will only break after the n-th time its been passed SwiftStriker00, on 12 February 2011 - 09:53 AM, said: Also in VS 2010 you can set conditional break points and hit count break points. These tools can be useful in loops especially if you dont want have to step through every iteration You can read more on the MSDN Page a word to the wise: conditional break points wont throw compiler errors, only runtime. Also make sure you do == and not = because it will actually set the variable and never break. The hit count break point is great in a loop because if your looping 1000 times, and your concerned with the 700th you dont want to hit continue 700 times. This break point will only break after the n-th time its been passed

I will tell ya from experience that conditional breakpoints seriously slow down execution. I was doing an image processing block, scanning all the pixels from left to right. The entire block on a big photos normally runs in 30 seconds. With the conditional breakpoint it took over two minutes just to conditionally break at row 50, column 100. For something like that I can build a conditional break that is FAR faster
1 if (x == 50 && y== 100) 2{ 3 console.writeline("something to break at");// put breakpoint here 4}

You might also like