M220N (.NET) Final Exam Question 6 is incorrect

The question is:
The MongoDB C# driver offers synchronous and asynchronous versions of most methods. If you are building methods that will use the asynchronous driver methods, which of the following are required ?
and the answer options are

  • adding the async keyword to the method signature
  • using the await keyword when calling the asynchronous method
  • ensuring your method returns a Task object
  • adding the await keyword to the method signature
  • ensuring your method signature includes a CancellationToken parameter
  • naming your method name to include “Async”

And the 1st, 2nd and 3rd are considered a correct answer which is not really the case.

I can agree with the third one - ‘ensuring your method returns a Task object’ - but even this is not a strict requirement - your method can return void, although it’s considered a really really bad practice (with some exceptions: e.g., it’s OK, if it’s some event handler).

About the 1st and 2nd: it’s not really necessary to use async/await keywords if you aren’t going to use the results of the async driver method within your method and simply want to return the task that is returned from the driver method. I mean we can even rewrite the example from the detailed answer without using async/await keywords:
here is the example:

public async Task<Foo> GetFoo(string movieId)
{
     var bar = await collection.FindAsync(...)
}

And we can write it in this way:

public Task<Foo> GetFoo(string movieId)
{
     return collection.FindAsync(...)
}

So, I believe this question and it’s answer(s) should be formulated in some other way.

1 Like