三元运算符在 C# 中的工作原理 [英] How Ternary operator works in C#

查看:24
本文介绍了三元运算符在 C# 中的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试了 unity,并在其中一个 youtube 视频剪辑中看到了他们的代码如下:

i recently try out unity and saw in one of the youtube video clip they code like this:

    Random.Range(0, 2) == 0 ? -1 : 1;

我理解 Random.Range() 但后面的部分是什么意思?提前谢谢你

i understand the Random.Range() but what does the the part after mean? thank you in advance

推荐答案

Its the C# 中的三元运算符.

示例:

condition ? expressionResultForTrue : expressionResultForFalse

如果 Random.Range(0, 2) == 0 计算结果为 True 我们得到 -1 else 1代码>

If Random.Range(0, 2) == 0 evaluates to True we get -1 else 1

这篇关于三元运算符在 C# 中的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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