Suppose I have code that looks like this:
public async Task DoSomethingReturnString(int n) { ... }
int[] numbers = new int[] { 1, 2 , 3};
If calling from an asynchronous method, you can write a wrapper method that creates a new dictionary and builds a dictionary by iterating over each number, calling your DoSomethingReturnString in turn:
public async Task CallerAsync()
{
int[] numbers = new int[] { 1, 2, 3 };
Dictionary dictionary = await ConvertToDictionaryAsync(numbers);
}
public async Task> ConvertToDictionaryAsync(int[] numbers)
{
var dict = new Dictionary();
for (int i = 0; i < numbers.Length; i++)
{
var n = numbers[i];
dict[n] = await DoSomethingReturnString(n);
}
return dict;
}