/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/operators/snippets/patterns/VarPattern.cs
45 строк
1 KB
Bill Wagner
Cover list patterns (#29154)
26 апр 2022, 18:47
Не верифицирован
26 апр 2022, 18:47
b181812
Код
Авторство
О чём код?
namespace Patterns; public static class VarPattern { public static void Examples() { TestTransform(); } private static void KeepInterimResult() { // <KeepInterimResult> static bool IsAcceptable(int id, int absLimit) => SimulateDataFetch(id) is var results && results.Min() >= -absLimit && results.Max() <= absLimit; static int[] SimulateDataFetch(int id) { var rand = new Random(); return Enumerable .Range(start: 0, count: 5) .Select(s => rand.Next(minValue: -10, maxValue: 11)) .ToArray(); } // </KeepInterimResult> } // <WithCaseGuards> public record Point(int X, int Y); static Point Transform(Point point) => point switch { var (x, y) when x < y => new Point(-x, y), var (x, y) when x > y => new Point(x, -y), var (x, y) => new Point(x, y), }; static void TestTransform() { Console.WriteLine(Transform(new Point(1, 2))); // output: Point { X = -1, Y = 2 } Console.WriteLine(Transform(new Point(5, 2))); // output: Point { X = 5, Y = -2 } } // </WithCaseGuards> }