Why am I not able to use stopwatch.Restart()?
I'm guessing you use pre 4.0 framework, which means you'll have to use Reset
and Start
instead of Restart
.
I'm guessing you are using .Net Framework 3.5
or below where the Restart
method of Stopwatch
doesnt exists.
If you want to replicate the same behavior you can do it like this.
Stopwatch watch = new Stopwatch();
watch.Start();
// do some things here
// output the elapse if needed
watch = Stopwatch.StartNew(); // creates a new Stopwatch instance
// and starts it upon creation
The StartNew static method already exists on .Net Framework 2.0
More details about StartNew method here: Stopwatch.StartNew Method
Or alternatively, you can create an extension method for yourself.
Here is a mockup and usage.
public static class ExtensionMethods
{
public static void Restart(this Stopwatch watch)
{
watch.Stop();
watch.Start();
}
}
Consume like
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Restart(); // an extension method
}
}