Usually when I have await in a scope of a method I need to mark method as async and return Task<>. In a code below compiler allowed me to resolve an async/await action inside a dictionary, I was wondering if it is safe to return void from a method in such case. Please consider a following code:
public void Consume(int ServiceId, MyContext ctx){
var consumerProvider = new Dictionary<int, Action<SomeClass>>{
{1,
async (consumerContext) => await FirstService.ConsumeAsync(consumerContext)},
{2,
async (consumerContext) => await SecondService.ConsumeAsync(consumerContext)},
};
consumerProvider[serviceId](ctx);
}
Again, I am just interested to learn if such wrapped async/await in the dictionary is safe or Consume method should return something else than void, and be marked as async.
I am not interested in refactoring this code not to face this challenge or to improve code quality. I am seeking to deeper understand async/await that is wrapped in some data structure, such as Dictionary.