Stopwatch Constructor
Collapse the table of content
Expand the table of content

Stopwatch Constructor ()


Initializes a new instance of the Stopwatch class.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

public Stopwatch()

The returned Stopwatch instance is stopped, and the elapsed time property of the instance is zero.

Use the Start method to begin measuring elapsed time with the new Stopwatch instance. Use the StartNew method to initialize a new Stopwatch instance and immediately start it.

The following example initializes a Stopwatch instance by using a simple class constructor.

using System;
using System.Diagnostics;
using System.Threading;
class Program
    static void Main(string[] args)
        Stopwatch stopWatch = new Stopwatch();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft