C# 具有多个选项的内联if-else语句

C# 具有多个选项的内联if-else语句,c#,if-statement,unity3d,inline,C#,If Statement,Unity3d,Inline,所以,我在看内联if语句,它使用了三元运算符。基本上,这是我当前的代码,我想使其更加紧凑 private void Move(Vector3 direction) { if(direction != Vector3.back && direction != Vector3.forward) transform.Rotate(0,(direction == Vector3.right ? 90 : -90),0); else tr

所以,我在看内联if语句,它使用了三元运算符。基本上,这是我当前的代码,我想使其更加紧凑

private void Move(Vector3 direction) {
    if(direction != Vector3.back && direction != Vector3.forward) 
        transform.Rotate(0,(direction == Vector3.right ? 90 : -90),0);
    else 
        transform.Rotate(0,(direction == Vector3.back ? 180 : 0),0);

    transform.Translate(Vector3.forward, Space.Self);
}
我真正想要的是这样的东西:

private void Move(Vector3 direction) {
    transform.Rotate(0,(direction == Vector3.right ? 90 : -90 || direction == Vector3.back ? 180 : 0),0);
    transform.Translate(Vector3.forward, Space.Self);
}
有什么办法可以这样做吗?举个例子。我想知道如何压缩多个内联if语句,所以如果可以避免的话,我不必无缘无故地有更多的代码行


感谢您抽出时间阅读我的问题。

我认为第一个问题足够紧凑。如果
Vector3
枚举有4个值,则第二个示例将不起作用。让它发挥作用看起来可能和第一个例子一样长

private void Move(Vector3 direction)
{
    transform.Rotate(0,
        direction == Vector3.right ? 90 :
            (direction == Vector3.left ? -90
                (direction == Vector3.back ? 180 : 0)), 0);
    ...
}
当您只有两个值要测试时,三值运算是最“紧凑”的

例如:

Color color;

if (title == "VIP")
    color = Color.Red;
else
    color = Color.Blue;
变成:

var color = (title == "VIP" ? Color.Red : Color.Blue);

这并不完全是您所要求的,但为了使方法更紧凑,或许可以尝试以下方法:

public enum Direction
{
   Left = -90,
   Right = 90,
   Forward =0,
   Back = 180
}

private void Move(Direction direction) 
{
   transform.Rotate(0,(int)direction,0);
   transform.Translate(Vector3.forward, Space.Self);
}

这正是我需要的。非常紧凑,非常可扩展。我很感激,我甚至没有想到这一点。你的方法是正确的,但不是很紧凑。这就是为什么我没有根据紧凑性和可扩展性来选择你的答案。下面的答案是我所接受的,因为它的想法简单、紧凑、可论证。但是我感谢你的时间和努力!