C# Intermediate course Exercise-1: Please give feedback on my finished solution : Design a Stopwatch

It’s pretty frustrating that Josh stopped showing his solutions. Now we have nothing to compare our solution to the ‘ideal’ solution. Could people please review my solution and ideally provide their own, so I can see how others have implemented this solution. Please also feel free to feedback to me on feedbacks, how to make the code clearer and more optimized.

namespace exe1
{
    public class StopWatch
    {
        private TimeSpan _start;
        private TimeSpan _stop;
        private TimeSpan _started;
        private TimeSpan _stopped;
        private TimeSpan _difference;
        private string? _input;
        private bool IsTimerRunning;

        public StopWatch() 
        {
            IsTimerRunning = false;
            
        }

        public void Interface(string button)
        {
            _input = button.ToLower();

            if (string.IsNullOrEmpty(_input))
            {
                throw new ArgumentException(nameof(_input));
            }
            if(_input == "start" && IsTimerRunning == false)
            {
                IsTimerRunning = true;
                _started = StartTimer();
                Console.WriteLine("Timer Started.");
            }
            else if(_input == "stop" && IsTimerRunning == true)
            {
                IsTimerRunning = false;
                _stopped = StopTimer();
                Console.WriteLine("Timer Stopped.");
                Console.WriteLine("Duration: " + Duration(_started, _stopped));
            } 
            else if(IsTimerRunning)
            {
                throw new InvalidOperationException(nameof(IsTimerRunning));
            }
        }

        private TimeSpan StartTimer()
        {
            _start = new TimeSpan(DateTime.Now.Ticks);
            return _start;
        }

        private TimeSpan StopTimer()
        {
            _stop = new TimeSpan(DateTime.Now.Ticks);
            return _stop;
        }

        private TimeSpan Duration(TimeSpan Start, TimeSpan Stop)
        {
            _difference = Stop.Subtract(Start);
            return _difference;
        }

    }
}

namespace exe1
{

    internal class Program
    {
        static void Main(string[] args)
        {

            var timer = new StopWatch();
            while (true)
            {
                Console.WriteLine("Enter the Word \"Start\" to begin timer, and \"Stop\" to stop timer.");
                var input = Console.ReadLine();
                timer.Interface(input);
            }



        }
    }
}

No Feedback from anyone?

I’m learning coding too so take my feedback with a grain of salt.

I don’t think you need the constructor because I believe bools are initialized to a false value by default.

I made start and stop assign a DateTime (DateTime.Now) instead, since subtracting 2 DateTimes returns a TimeSpan.

I did mine as a console app, here’s my code

var timer = new Stopwatch();

while(true)
{
    Console.Write("Enter a Command (start, stop, duration, quit): ");
    var input = Console.ReadLine().ToLower();
    if (input == "start")
        timer.Start();
    else if (input == "stop")
        timer.Stop();
    else if (input == "duration")
        timer.Duration();
    else if (input == "quit")
        return;
    else
    {
        Console.WriteLine("Invalid Input, Try Again");        
    }
}

public class Stopwatch
{
    private bool _isRunning;
    private DateTime _startTime;
    private DateTime _endTime;
    private TimeSpan _duration;

    public void Start()
    {
        if (_isRunning) throw new InvalidOperationException("_isRunning");
        _startTime = DateTime.Now;
        _isRunning = true;
    }

    public void Stop()
    {
        if (!_isRunning) throw new InvalidOperationException("_isRunning");
        _endTime = DateTime.Now;
        _isRunning = false;
    }

    public void Duration()
    {
        if (_isRunning) throw new InvalidOperationException("Stopwatch is running");
        _duration = _endTime - _startTime;
        Console.WriteLine("Duration is: " + _duration.ToString(@"hh\:mm\:ss\:FF"));
    }
}```