在C#中,如何使用较短的方法(带?)表示以下if else语句:
if (condition1 == true && count > 6)
{
dothismethod(value);
}
else if (condition2 == false)
{
dothismethod(value);
}
Run Code Online (Sandbox Code Playgroud)
我的代码看起来非常混乱这些陈述.有人可以指导我一个很好的资源,如果那么其他捷径语法?
SLa*_*aks 19
这听起来像你正在努力写作
if ((condition1 && count > 6) || !condition2)
SomeMethod();
Run Code Online (Sandbox Code Playgroud)
?如果是/否则不是"捷径".它被称为三元运算符,当您想根据条件为某个变量赋值时使用它,如下所示:
string message = hasError ? "There's an error!" : "Everything seems fine...";
MSDN:http://msdn.microsoft.com/en-us/library/ty67wk28%28v=vs.100%29.aspx