简写如果声明:C# [英] shorthand If Statements: C#

查看:109
本文介绍了简写如果声明:C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个快速的,反正是有速记本?

Just a quick one, Is there anyway to shorthand this?

这基本上确定了方向右左或右,1为左,0

It's basically determining the direction left or right, 1 for left, 0 for right

在C#中:

if (column == 0) { direction = 0; }
else if (column == _gridSize - 1) { direction = 1; }
else { direction = rand.Next(2); }



语句下面这将是:

The statement following this will be:

if (direction == 1)
{
    // do something
}
else
{
    // do something else
}

如果没有,它并没有真正物!只是好奇:)

If there isn't, it doesn't really matter! just curious:)

推荐答案

要使用简写来获得方向:

To use shorthand to get the direction:

int direction = column == 0
                ? 0
                : (column == _gridSize - 1 ? 1 : rand.Next(2));

要简化完全的代码:

if (column == gridSize - 1 || rand.Next(2) == 1)
{
}
else
{
}

这篇关于简写如果声明:C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆