Skip to content

Instantly share code, notes, and snippets.

@bolenton
Created November 14, 2016 08:08
Show Gist options
  • Save bolenton/fdb97131f9714c101dc20c23eae8ad86 to your computer and use it in GitHub Desktop.
Save bolenton/fdb97131f9714c101dc20c23eae8ad86 to your computer and use it in GitHub Desktop.
namespace StopWatch
{
public class StopWatch
{
private DateTime StartTime { get; set; }
public void Start() => this.StartTime = DateTime.Now;
public TimeSpan Stop() => DateTime.Now.Subtract(StartTime);
}
internal class Program
{
private static void Main(string[] args)
{
ConsoleKey userChoice;
do
{
Console.WriteLine("\n\nPress the 'Space' key to Start timer, any other button to exit.");
userChoice = Console.ReadKey().Key;
if(userChoice != ConsoleKey.Spacebar)break;
var watch = new StopWatch();
watch.Start();
Console.WriteLine("\nTimer Started.. press Space to Stop timer, any other button to exit.");
userChoice = Console.ReadKey().Key;
var duration = watch.Stop();
Console.WriteLine($"\nDuration: {duration.Hours}:{duration.Minutes}:{duration.Seconds}");
} while (userChoice == ConsoleKey.Spacebar);
Console.WriteLine("\nExiting timer....");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment