/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/operators/snippets/shared/TrueFalseOperators.cs
58 строк
2 KB
Bill Wagner
first draft of nameof updates (#29549)
15 июн 2022, 00:43
Не верифицирован
15 июн 2022, 00:43
86ba43a
Код
Авторство
О чём код?
public struct LaunchStatus { public static readonly LaunchStatus Green = new LaunchStatus(0); public static readonly LaunchStatus Yellow = new LaunchStatus(1); public static readonly LaunchStatus Red = new LaunchStatus(2); private int status; private LaunchStatus(int status) { this.status = status; } public static bool operator true(LaunchStatus x) => x == Green || x == Yellow; public static bool operator false(LaunchStatus x) => x == Red; public static LaunchStatus operator &(LaunchStatus x, LaunchStatus y) { if (x == Red || y == Red || (x == Yellow && y == Yellow)) { return Red; } if (x == Yellow || y == Yellow) { return Yellow; } return Green; } public static bool operator ==(LaunchStatus x, LaunchStatus y) => x.status == y.status; public static bool operator !=(LaunchStatus x, LaunchStatus y) => !(x == y); public override bool Equals(object obj) => obj is LaunchStatus other && this == other; public override int GetHashCode() => status; } public class LaunchStatusTest { public static void Main() { LaunchStatus okToLaunch = GetFuelLaunchStatus() && GetNavigationLaunchStatus(); Console.WriteLine(okToLaunch ? "Ready to go!" : "Wait!"); } static LaunchStatus GetFuelLaunchStatus() { Console.WriteLine("Getting fuel launch status..."); return LaunchStatus.Red; } static LaunchStatus GetNavigationLaunchStatus() { Console.WriteLine("Getting navigation launch status..."); return LaunchStatus.Yellow; } }