Microsoft Exam Questions

Which code segment will properly return the TimeSpan returned by the stopWatch variable?

Which code segment will properly return the TimeSpan returned by the stopWatch variable?

A.
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
DoSomething();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.WriteLine(elapsedTime, “RunTime”);
private void DoSomething()
{ … }

B.
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
DoSomething();
stopWatch.Reset();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.WriteLine(elapsedTime, “RunTime”);
private void DoSomething()
{ … }

C.
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
DoSomething();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.WriteLine(elapsedTime, “RunTime”);
private void DoSomething()
{ … }

D.
Stopwatch stopWatch = new Stopwatch();
stopWatch.Begin();
DoSomething();
stopWatch.End();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.WriteLine(elapsedTime, “RunTime”);
private void DoSomething()
{ … }

Explanation:
Stopwatch Class
(http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx)