Make a Stopwatch C#

8 June, 2019

Step 1

Import System.Diagnostics:

using System.Diagnostics;

Step 2

Initialise the timer & start it:

Stopwatch timer = new Stopwatch();
timer.Start();

Step 3

Stop the timer:

timer.Stop();

Step 4

Display stopwatch results:

Console.WriteLine(overallTimer.Elapsed.Seconds.ToString() + "s");

You can use a multiple amount of different timings such as:

  • Milliseconds
  • Minutes
  • Hours
  • Days

You can put ‘Total’ before the unit of timing to give the exact amount of time taken:

Console.WriteLine(overallTimer.Elapsed.TotalSeconds.ToString() + "s");