Skip to content

Instantly share code, notes, and snippets.

@mttchpmn
Last active February 13, 2021 01:29
Show Gist options
  • Save mttchpmn/855f4d54662d27cf032686803d56b5b7 to your computer and use it in GitHub Desktop.
Save mttchpmn/855f4d54662d27cf032686803d56b5b7 to your computer and use it in GitHub Desktop.
C# Stopwatch Class
using System;
namespace Sandbox
{
public class Stopwatch
{
private bool _started;
private DateTime _startTime;
public TimeSpan Duration { get; private set; }
public void Start()
{
if (_started) throw new InvalidOperationException("Stopwatch already started.");
_started = true;
_startTime = DateTime.Now;
}
public void Stop()
{
if (!_started) throw new InvalidOperationException("Stopwatch not started.");
this.Duration += (DateTime.Now - _startTime);
_started = false;
}
public void Reset()
{
_started = false;
this.Duration = TimeSpan.Zero;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment