将匿名任务添加到此列表的语法是什么



我有一些代码如下所示:

async void MyMethod()
{
var client = new HttpClient();
var tasks = new List<Task<string>>();
for (int id = 1; id <= 10; id++)
{
tasks.Add(GetValue(client, id));
}  
await Task.WhenAll(tasks);
}
async Task<string> GetValue(HttpClient client, int id)
{
var response = await client.GetAsync($"http://example.com?id={id}");
// Do something else here so that this task can't be condensed to a 
// single line of code
System.Diagnostics.Debug.WriteLine("something");
return await response.Content.ReadAsStringAsync();
}

在不使用GetValue方法的情况下向tasks添加匿名任务的语法是什么?我想做一些类似下面代码的事情,但我不知道从我的匿名方法返回Task<string>的语法(这段代码给了我一个编译错误Cannot convert async lambda expression to delegate type Func<string>(:

for (int id = 1; id <= 10; id++)
{
tasks.Add(new Task<string>(async () =>
{
var response = await client.GetAsync($"http://example.com?id={id}");
// Do something else here so that this task can't be condensed to a 
// single line of code
System.Diagnostics.Debug.WriteLine("something");
return await response.Content.ReadAsStringAsync();
}));
}

执行委托的方式非常简单,只需调用即可。

public static T Execute<T>(Func<T> func)
{
return func();
}

for (int id = 1; id <= 10; id++)
{
tasks.Add(Execute(async () =>
{
var response = await client.GetAsync($"http://example.com?id={id}");
return await response.Content.ReadAsStringAsync();
}));
}