Last active
February 13, 2021 01:29
-
-
Save mttchpmn/855f4d54662d27cf032686803d56b5b7 to your computer and use it in GitHub Desktop.
C# Stopwatch Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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