freeCodeCamp/guide/russian/csharp/ternary-operator/index.md

41 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Ternary operator
localeTitle: Тернарный оператор
---
# Тернарный оператор ( `?:` :)
Тернарный оператор возвращает одно из двух выражений, основанное на условии. Его можно использовать в качестве ярлыка для выражения if ... else.
## Синтаксис
```
condition_expression ? expression_1 : expression_2
```
### параметр
`condition_expression` Булевое выражение.
`expression_1` Возвращается, если выражение `condition_expression` истинно.
`expression_2` Возвращается, если выражение `condition_expression` равно false.
## пример
```
// initialize - set true or false here to view different result
bool hasFreeSweet = false;
string str = hasFreeSweet ? "Free sweet!" : "No free sweet.";
//output in console
Console.WriteLine(str);
```
## Вывод
```
if hasFreeSweet == true
> Free sweet!
if hasFreeSweet == false
> No free sweet.
```