/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/operators/snippets/shared/ConditionalOperator.cs
85 строк
2 KB
YUVRAJ N JADHAV
Update ConditionalOperator.cs (#40547)
18 апр 2024, 16:31
Не верифицирован
18 апр 2024, 16:31
c0ce326
Код
Авторство
О чём код?
namespace operators; public class ConditionalOperator { public static void Examples() { BasicExample(); ConditionalRefExpressions(); ComparisonWithIf(); TargetTyped(); NotTargetTyped(); } private static void BasicExample() { // <BasicExample> string GetWeatherDisplay(double tempInCelsius) => tempInCelsius < 20.0 ? "Cold." : "Perfect!"; Console.WriteLine(GetWeatherDisplay(15)); // output: Cold. Console.WriteLine(GetWeatherDisplay(27)); // output: Perfect! // </BasicExample> } private static void ConditionalRefExpressions() { // <SnippetConditionalRef> int[] smallArray = {1, 2, 3, 4, 5}; int[] largeArray = {10, 20, 30, 40, 50}; int index = 7; ref int refValue = ref ((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]); refValue = 0; index = 2; ((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]) = 100; Console.WriteLine(string.Join(" ", smallArray)); Console.WriteLine(string.Join(" ", largeArray)); // Output: // 1 2 100 4 5 // 10 20 0 40 50 // </SnippetConditionalRef> } private static void TargetTyped() { // <SnippetTargetTyped> var rand = new Random(); var condition = rand.NextDouble() > 0.5; int? x = condition ? 12 : null; IEnumerable<int> xs = x is null ? new List<int>() { 0, 1 } : new int[] { 2, 3 }; // </SnippetTargetTyped> } private static void NotTargetTyped() { // <SnippetNotTargetTyped> var rand = new Random(); var condition = rand.NextDouble() > 0.5; var x = condition ? 12 : (int?)null; // </SnippetNotTargetTyped> } private static void ComparisonWithIf() { // <SnippetCompareWithIf> int input = new Random().Next(-5, 5); string classify; if (input >= 0) { classify = "nonnegative"; } else { classify = "negative"; } classify = (input >= 0) ? "nonnegative" : "negative"; // </SnippetCompareWithIf> } }