Consider the following interface and implementations.
interface IService
{
Task<string> GetAnswer(string question);
}
class SomeService : IService
{
async Task<string> IService.GetAnswer(string question)
{
... code using awaits ...
}
}
class AnotherService : IService
{
Task<string> IService.GetAnswer(string question)
{
return Task.FromResult("I have no idea.");
}
}
According to the Microsoft naming conventions, should the interface method be named GetAnswer
or GetAnswerAsync
?
By convention, you append "Async" to the names of methods that have an Async or async modifier.
The problem is that the first implementation uses the async
modifier, indicating it should receive the "Async" method name suffix, but the second implementation does not use the async
modifier, indicating it should not receive the "Async" method name suffix. The two method names in the implementations are forced to be the same by the interface, so I am forced to violate the naming conventions for one of the two classes.
Note I'm not looking for opinionated answers. Consider it multiple choice. :)
Async methods can have the following return types: Task, for an async method that performs an operation but returns no value. Task<TResult>, for an async method that returns a value. void , for an event handler.
By convention, you append "Async" to the names of methods that have an async modifier. You can ignore the convention where an event, base class, or interface contract suggests a different name. For example, you shouldn't rename common event handlers, such as Button1_Click .
Async code uses Task<T> and Task , which are constructs used to model work being done in the background. The async keyword turns a method into an async method, which allows you to use the await keyword in its body.
Async and await in C# are the code markers, which marks code positions from where the control should resume after a task completes. Let's start with practical examples for understanding the programming concept.
You should use XAsync
even when there isn't an async
modifier as long as the method represents a complete task based asynchronous operation.
To be technical about it, the passage you quoted tells you to add an Async when there is an async
modifier but doesn't tell what to do when there isn't any.
The async
modifier is really not a part of the method's signature and you could easily accomplish the exact same behavior without it. If you look at the Task-based Asynchronous Pattern you wouldn't find a reference to the specific async
modifier, but to the more broad definition of an async
method.
In the .Net framework itself, you can't even know which Async method actually uses the async
modifier. A lot (if not most) return TaskCompletionSource.Task
to allow you (as a user) to use async-await
. For example, this is Stream.WriteAsync
:
public virtual Task WriteAsync(Byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
// If cancellation was requested, bail early with an already completed task.
// Otherwise, return a task that represents the Begin/End methods.
return cancellationToken.IsCancellationRequested
? Task.FromCancellation(cancellationToken)
: BeginEndWriteAsync(buffer, offset, count);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With