I am trying to make a stopwatch with C# as an exercise for myself. my plan was to make two methods " start()" and " stop()" then call these from my stopwatch class in my main. the problem I have is that I do not know how to get the time span between these two.
for your information, this is how I want the program to work: if they typed s the timer starts and when press enter or type f the time will be shown to them.
this is the code I have written so far, but got stuck when getting the time span.
class StopWatch
{
DateTime starting = DateTime.Now;
DateTime finishing = DateTime.Now;
public void start()
{
Console.WriteLine(starting);
}
public void stop()
{
Console.WriteLine(finishing);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("type s to start and f to stop");
var input = Console.ReadLine();
var stopwatch = new StopWatch();
if (input.ToLower() == "s") { stopwatch.start(); }
var Input2 = Console.ReadLine();
if (Input2.ToLower() == "f") { stopwatch.stop(); }
Console.ReadKey();
}
}