Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between creating a new instance with "new() and ".StartNew()"?

Coming from my "answer" to question "Stopwatch in a Task seems to be additive across all tasks, want to measure just task interval"

What are the possible differences between creating a new Stopwatch instance as:

Stopwatch timer = System.Diagnostics.Stopwatch.StartNew();

with

Stopwatch timer = new Stopwatch();
timer.Start();  

Implied subquestion:
Why was StartNew() method provided?

like image 346

1 Answers

It initializes a new System.Diagnostics.Stopwatch instance and sets the elapsed time property to zero, and starts measuring elapsed time

and save the one line code to implicitly calling Start() method

like image 141
Mukesh Pareek Avatar answered Oct 01 '22 06:10

Mukesh Pareek