我尝试实现了C# 8.0的switch-case语句,但不幸的是它没有工作,
如果case被满足,我想返回一个特定的字符串来表示满足该case的switch表达式。
以下是我的代码:
public static void GetErrMsg(Exception ex) =>
ex switch
{
ex is UserNotFoundException => "User is not found.",
ex is NotAuthorizedException => "You'r not authorized."
};
但我得到了以下消息:
错误 CS0201 仅赋值、调用、增量、减量、等待和新对象表达式可用作语句。
错误 CS0029 无法将类型为“bool”的值隐式转换为“System.Exception”
请注意,这些错误信息是由编译器生成的,可能需要检查代码以找出问题所在。
void
,而是返回string
。 - MakePeaceGreatAgainCS0201
是因为你的GetErrMsg
方法返回的是void
,而不是string
。至于CS0029
,请参见 @Marc 的答案。 - canton7