Archive

Archive for May, 2010

Stopwatch: Timing operations easily with precisions

The System.Diagnostics namespace define a lot of classes.

Stopwatch is one great class that allow to time operations with precision.

Usage is really simple. Create a Stopwatch instance and use the Start() and Stop() methods to start and stop the counter. Use the ElapsedMilliseconds,


Stopwatch timer = new Stopwatch();
timer.Start();
Thread.Sleep(10000);
timer.Stop();
Trace.WriteLine(timer.ElapsedMilliseconds);
Trace.WriteLine(timer.Elapsed);
Trace.WriteLine(timer.ElapsedTicks);

Output:
10005
00:00:10.0051010
25990391

You can use the Reset method to reinitialize the counter.

Here is the MSDN link to the Stopwatch class

Categories: C#

Disable method step-into with debugger

The attribute DebuggerHiddenAttribute indicate to the Visual Studio debugger that the method is not debuggable, so you can’t step in.

[DebuggerHiddenAttribute]
public void DoThis()
{
}

Categories: C#, VisualStudio

Enhancing debugging experinence with Debugger Display Attributes

Have you ever wrote a class with a lot of properties/members and using the debugger was unefficient due to the among of stuffs showed and the lack of formatting of the informations?

Thanks to the attributes, you can decorate the members and properties to change the default apparence and behaviour in the debugger.

Let’s say that you wrote this simple class:

        public class Contact
        {
            public Contact(string name)
            {
                this.Name = name;
            }

            public string Name { get; set; }
            public List<string> Properties { get { return this.properties; } }

            private List<string> properties = new List<string>();
        }

Let’s use our newly created class:

        static void Main(string[] args)
        {
            Contact c1 = new Contact("JF");
            c1.Properties.Add("P1");
            c1.Properties.Add("P2");
        }


Hiding unwanted properties/members

You can see in the debugger that, the properties and Properties are in a way duplicated:

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private List properties = new List();

Note that DebuggerBrowsable attribute is defined in System.Diagnostics.

As stated in the MSDN documentation, DebuggerBrowsable constructor take a DebuggerBrowsableState enumeration values. The 3 values defined are:

    Never indicates that the member is not displayed in the data window. For example, using this value for the DebuggerBrowsableAttribute on a field removes the field from the hierarchy; the field is not displayed when you expand the enclosing type by clicking the plus sign (+) for the type instance.
    Collapsed indicates that the member is displayed but not expanded by default. This is the default behavior.
    RootHidden indicates that the member itself is not shown, but its constituent objects are displayed if it is an array or collection.


DebuggerDisplay

You can also change the value formatting of every field/properties.

Imagine that you have a class and you want to have Count = 3 written without having to expand the class. you can use the Debugger display attribute to acheive it:

[DebuggerDisplay("Count = {count}")]
class MyClass 
{
    public int count = 3;
}

Categories: C#, VisualStudio