/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/operators/snippets/patterns/LogicalPatterns.cs
86 строк
3 KB
Bill Wagner
Add more details on pattern binding (#43574)
15 ноя 2024, 23:17
Не верифицирован
15 ноя 2024, 23:17
e49964b
Код
Авторство
О чём код?
namespace Patterns; public static class LogicalPatterns { public static void Examples() { AndPattern(); OrPattern(); } private static void NotPattern(object input) { // <NotPattern> if (input is not null) { // ... } // </NotPattern> } private static void AndPattern() { // <AndPattern> Console.WriteLine(Classify(13)); // output: High Console.WriteLine(Classify(-100)); // output: Too low Console.WriteLine(Classify(5.7)); // output: Acceptable static string Classify(double measurement) => measurement switch { < -40.0 => "Too low", >= -40.0 and < 0 => "Low", >= 0 and < 10.0 => "Acceptable", >= 10.0 and < 20.0 => "High", >= 20.0 => "Too high", double.NaN => "Unknown", }; // </AndPattern> } private static void OrPattern() { // <OrPattern> Console.WriteLine(GetCalendarSeason(new DateTime(2021, 1, 19))); // output: winter Console.WriteLine(GetCalendarSeason(new DateTime(2021, 10, 9))); // output: autumn Console.WriteLine(GetCalendarSeason(new DateTime(2021, 5, 11))); // output: spring static string GetCalendarSeason(DateTime date) => date.Month switch { 3 or 4 or 5 => "spring", 6 or 7 or 8 => "summer", 9 or 10 or 11 => "autumn", 12 or 1 or 2 => "winter", _ => throw new ArgumentOutOfRangeException(nameof(date), $"Date with unexpected month: {date.Month}."), }; // </OrPattern> } // <NegationWithoutParens> // Incorrect pattern. `not` binds before `and` static bool IsNotLowerCaseLetter(char c) => c is not >= 'a' and <= 'z'; // </NegationWithoutParens> // <DefaultBinding> // The default binding without parentheses is shows in this method. `not` binds before `and` static bool IsNotLowerCaseLetterDefaultBinding(char c) => c is ((not >= 'a') and <= 'z'); // </DefaultBinding> // <SpecifyBindingOrder> // Correct pattern. Force `and` before `not` static bool IsNotLowerCaseLetterParentheses(char c) => c is not (>= 'a' and <= 'z'); // </SpecifyBindingOrder> // <WithParentheses> static bool IsLetter(char c) => c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z'); // </WithParentheses> private static void ChangedPrecedence(object input) { // <ChangedPrecedence> if (input is not (float or double)) { return; } // </ChangedPrecedence> } }