So I have this method that has only synchronous operations:
public void Foo(){
//do something synchronously
if(x == 0)
return;
else if(x < 5):
//do something
return;
// do something extra
}
Due to some interface changes, that method has to be changed to asynchronous, despite having no async operations. And in order to have the async keyword, there must be an await
keyword within its body. Would an equivalent method of the originally synchronous method be:
public async Task FooAsync(){
//do something synchronously
if(x == 0)
await Task.CompletedTask;
else if(x < 5):
//do something
await Task.CompletedTask;
// do something extra
}
I'm not sure about this since the CompletedTask
property indicates that it is a successful operation, which isn't the case for all these conditionals.
EDIT: Resolved in comments. Just because a method is denoted with an "async" suffix it does not mean that it needs an async
modifier. The function would be written as shown below to adhere to interface changes:
public Task FooAsync(){
//do something synchronously
if(x == 0)
return Task.CompletedTask;
else if(x < 5):
//do something
return Task.CompletedTask;
// do something extra
return Task.CompletedTask;
}