/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/fundamentals/functional/snippets/patterns/Simulation.cs
84 строки
2 KB
Bill Wagner
constant string pattern and Spans (#29707)
06 июн 2022, 22:25
Не верифицирован
06 июн 2022, 22:25
700b8d3
Код
Авторство
О чём код?
namespace patterns; public enum Operation { SystemTest, Start, Stop, Reset } public enum State { Off, Ready, Running } class Simulation { // <PerformOperation> public State PerformOperation(Operation command) => command switch { Operation.SystemTest => RunDiagnostics(), Operation.Start => StartSystem(), Operation.Stop => StopSystem(), Operation.Reset => ResetToReady(), _ => throw new ArgumentException("Invalid enum value for command", nameof(command)), }; // </PerformOperation> // <PerformStringOperation> public State PerformOperation(string command) => command switch { "SystemTest" => RunDiagnostics(), "Start" => StartSystem(), "Stop" => StopSystem(), "Reset" => ResetToReady(), _ => throw new ArgumentException("Invalid string value for command", nameof(command)), }; // </PerformStringOperation> // <PerformSpanOperation> public State PerformOperation(ReadOnlySpan<char> command) => command switch { "SystemTest" => RunDiagnostics(), "Start" => StartSystem(), "Stop" => StopSystem(), "Reset" => ResetToReady(), _ => throw new ArgumentException("Invalid string value for command", nameof(command)), }; // </PerformSpanOperation> // <RelationalPattern> string WaterState(int tempInFahrenheit) => tempInFahrenheit switch { (> 32) and (< 212) => "liquid", < 32 => "solid", > 212 => "gas", 32 => "solid/liquid transition", 212 => "liquid / gas transition", }; // </RelationalPattern> // <RelationalPattern2> string WaterState2(int tempInFahrenheit) => tempInFahrenheit switch { < 32 => "solid", 32 => "solid/liquid transition", < 212 => "liquid", 212 => "liquid / gas transition", _ => "gas", }; // </RelationalPattern2> private State ResetToReady() => State.Ready; private State StopSystem() => State.Off; private State StartSystem() => State.Running; private State RunDiagnostics() => State.Ready; }