【发布时间】:2016-06-22 13:12:26
【问题描述】:
我的任务是清理编译器警告的项目。目前我正在研究用于 ASP.NET 和 Xamarin 解决方案的 cs 文件。我有一个方法:
public override async SomeReturnTypeItem LoginAsync()
{
#if __MOBILE__
throw new NotSupportedException("Windows authentication is not supported in mobile version");
#endif
//some code
}
在 Xamarin 解决方案中,我警告说 #endif 下的代码无法访问。如果我用#else 替换#endif 并将#endif 放在方法的末尾,我会收到一条警告,指出方法缺少await 运算符并将同步运行。我怎样才能使这个方法没有警告?
【问题讨论】:
-
你期待什么?如果设置了
__MOBILE__,则代码是 无法访问- 一个异常和它之后的任何代码都不会执行。如果您不想收到警告,请删除代码,或使用#else指令 -
如果您收到警告“方法缺少等待运算符”`,您根本不应该有
async关键字,对于两个版本都去掉它。 -
假设“某些代码”包含
await是否安全?
标签: c# exception async-await c-preprocessor