Created
April 15, 2018 17:15
-
-
Save fdjones/b771c17c4169294e0560465238cd2ce2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Stopwatch.cs | |
using System; | |
namespace Methods | |
{ | |
public class Stopwatch | |
{ | |
private TimeSpan _initialTime; | |
private TimeSpan _stopTime; | |
private bool _hasBeenClicked; | |
public void Start() | |
{ | |
if (_hasBeenClicked) | |
{ | |
throw new System.InvalidOperationException(); | |
} | |
_hasBeenClicked = true; | |
_initialTime = DateTime.Now.TimeOfDay; | |
} | |
public TimeSpan Stop() | |
{ | |
_stopTime = DateTime.Now.TimeOfDay; | |
var spanTime = _stopTime.Subtract(_initialTime); | |
_hasBeenClicked = false; | |
return spanTime; | |
} | |
} | |
} | |
// Program.cs | |
using System; | |
using System.Threading; | |
namespace Methods | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
var watch = new Stopwatch(); | |
watch.Start(); | |
var firsTimeSpan = watch.Stop(); | |
Console.WriteLine("first timespan: {0}", firsTimeSpan); // first timespan: 00:00:00.0316230 | |
watch.Start(); | |
Thread.Sleep(1000); | |
var secondTimeSpan = watch.Stop(); | |
Console.WriteLine("second timespan: {0}", secondTimeSpan); // second timespan: 00:00:01.0052090 | |
watch.Start(); | |
Thread.Sleep(3000); | |
var thirdTmeSpan = watch.Stop(); | |
Console.WriteLine("third timespan: {0}", thirdTmeSpan); // third timespan: 00:00:03.0009240 | |
watch.Start(); | |
Thread.Sleep(1000); | |
var fourthTmeSpan = watch.Stop(); | |
Console.WriteLine("fourth timespan: {0}", fourthTmeSpan); | |
watch.Start(); | |
var fifthTimeSpan = watch.Stop(); | |
Console.WriteLine("fifth timespan: {0}", fifthTimeSpan); // first timespan: 00:00:00.0000020 | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
throw; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment