How can you wait for a Task to finish in C#?

Prepare for the Microsoft Certified Solutions Developer Certification Test. Use multiple choice quizzes with hints and explanations to boost your readiness. Excel on your test!

In C#, to wait for a Task to complete, you can simply call the Wait method on the task instance itself. When you use t.Wait(), you are invoking the Wait method of the Task class on the specific task instance denoted by 't'. This method will block the calling thread until the Task has completed its execution, which is a straightforward and effective way to ensure that you do not proceed until the desired Task has finished.

The other provided options do not reflect correct usage of the Task class in C#. Option B suggests using Task.Wait with an argument, which is not how the method is designed to be called; it only allows a call on an instance of a Task. Options C and D suggest methods that do not exist in the Task class, indicating a misunderstanding of the API. Therefore, using t.Wait() is the correct and recommended way to wait for a Task to finish in C#.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy