StopWatch for users C# I have tried to create a stopwatch and I have done but I don't know how to improve it, and I want the user control the stopwatch whether it (Start) or (Stop), I'll be thankful if you tell me what I can do to improve my code.

StopWatch.cs

public class StopWatch
{
    private DateTime _startTime;
    private DateTime _stopTime;
    private bool _timeRunning = false;

    public void Start(DateTime Start)
    {
        if (!_timeRunning)
        {
            _startTime = Start;
            _timeRunning = true;
        }
        else
            throw new InvalidOperationException("Stopwatch is already running!");
    }

    public void Stop(DateTime Stop)
    {
        if (_timeRunning)
        {
            _stopTime = Stop;
            _timeRunning = false;
        }
    }

    public TimeSpan GeTime()
    {
        var duration = _stopTime - _startTime;
        return duration;
    }
}


Program.cs

class Program
{
    static void Main(string[] args)
    {
        var stopwatch = new StopWatch();

        stopwatch.Start(DateTime.Now);

        for (int i = 0; i &lt; 1000; i++)
        {
            Thread.Sleep(10);
        }

        stopwatch.Stop(DateTime.Now);

        Console.WriteLine(@"Time Elapsed : {0:hh\:mm\:ss}", stopwatch.GeTime());
    }
Normally I wouldn't recommend creating a class with almost exactly the same name as a framework class (your StopWatch vs the existing Stopwatch), but you mentioned that this is an exercise, so let's ignore that for now.


The basic approach here seems fine: Start stores the start time, Stop stores the end time, and GetTime returns the difference. But there's a bug: GetTime does not work correctly if the stopwatch is still running. In that case, instead of looking at the (incorrect) stop time, it should look at the current time.
Why do Start and Stop take a DateTime as argument? That's something that the stopwatch itself should be doing. If, for some reason, you really must allow calling code to pass in their own times, then your stopwatch class should probably verify that the stop time is not earlier than the start time.
Why does Start throw an exception if the stopwatch is already running? Is that really something that should not be allowed? If so, why is it allowed to call Stop on a stopped stopwatch? I see no real reason for this inconsistency.
If starting a running stopwatch is not allowed, why can't a caller determine whether a stopwatch is running? A read-only IsRunning property that returns _timeRunning would be useful.
As you know, there's already a Stopwatch class in System.Diagnostics, so I would encourage you to look at its source code. You'll see several differences. One in particular is that it remembers the total elapsed time across multiple start/stop calls. Related to that are the Reset and Restart methods, and there's a convenient static StartNew method. It also uses lower-level methods for more accurate timing.