使用带有 async/await 关键字的最新 CTP5,我编写了一些代码,显然无法编译:
class Program
{
public class MyClass
{
async public Task<int> Test()
{
var result = await TaskEx.Run(() =>
{
Thread.Sleep(3000);
return 3;
});
return result;
}
}
static void Main(string[] args)
{
var myClass = new MyClass();
//The 'await' operator can only be used in a method or lambda marked with the 'async' modifier error ??!!
int result = await myClass.Test();
Console.ReadLine();
}
}
“'await' 运算符只能在标有 'async' 修饰符错误的方法或 lambda 中使用?”的原因是什么?(我选择了 Visual Studio 指向我的行)