官术网_书友最值得收藏!

The ? operator

The if-else statements are so common and widely used in C# that a specialized shorthand notation is available for writing simpler ones, without resorting to the full multiline if-else statements. This shorthand is called the ? operator. The basic form of this statement is as follows:

//If condition is true then do expression 1, else do expression 2
(condition) ? expression_1 : expression_2;

Let's see the ? operator in a practical example as shown here:

//We should hide this object if its Y position is above 100 units
bool ShouldHideObject = (transform.position.y > 100) ? true : false;

//Update object visibility
gameObject.SetActive(!ShouldHideObject);

Tip

The ? operator is useful for shorter statements, but for long and more intricate statements, it can make your code harder to read.

主站蜘蛛池模板: 荆州市| 隆化县| 巴南区| 如皋市| 连平县| 吐鲁番市| 新乡县| 察隅县| 宽城| 贺兰县| 南和县| 盘山县| 海阳市| 万宁市| 湟源县| 十堰市| 江阴市| 永年县| 贵阳市| 永安市| 河曲县| 桑日县| 无为县| 宿迁市| 武胜县| 鸡泽县| 抚州市| 英超| 桑日县| 东辽县| 黑龙江省| 什邡市| 浑源县| 元氏县| 炉霍县| 布尔津县| 珲春市| 历史| 丹东市| 周宁县| 景泰县|