我想了解异步如何工作。这是我的代码:在哪里以及如何放置等待关键字
class Program
{
static void Main(string[] args)
{
Task<string> strReturned = returnStringAsync();
Console.WriteLine("hello!");
string name = await strReturned; //error: The 'await' operator can only be used
//within an async method. Consider marking this
//method with the 'async' modifier and changing
//its return type to 'Task'
Console.WriteLine(name);
}
static async Task<string> returnStringAsync()
{
Thread.Sleep(5000);
return "Richard";
}
}
是否有错误?
那么,是什么?你不明白什么错误告诉你的? – poke
错误在strReturned旁边。 – Richard77
我看到了,但是错误信息很好地解释了你所尝试的是什么问题。那么你没有得到什么? – poke