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

时光毁灭记忆、已成空白 提交于 2019-12-05 04:47:34

StartNew, create new instance of the stop watch and starts it as well.

Simple new is Stopwatch instantiation only. It doesn't start the Stopwatch.

For your current code where you are creating a new instance and calling Start, there shouldn't be any difference to Stopwatch.StartNew()

See Stopwatch.StartNew() - MSDN

This method is equivalent to calling the Stopwatch constructor and then calling Start on the new instance.

StartNew provides an easier way to create and start Stopwatch in one step.

Why was StartNew() method provided?

It's a simpler alternative. StartNew is effectively the same as allocating a new stop watch and starting it instantly.

Since that's a very common use case, the StartNew factory method just simplifies and shortens the API, and reduces the code required to use a stopwatch.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!