Created
June 6, 2021 13:57
-
-
Save rstropek/377d6e82b60a415270553ab1e9a4e7a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
var numbers = new List<int>() { 1, 2, 3, 5, 8 }; | |
// List pattern | |
if (numbers is { 1, 2, 3, 5, 8 }) | |
{ | |
Console.WriteLine("Fibonacci"); | |
} | |
// Property pattern | |
if (numbers is { var first, 2, 3, 5, var last } && first == 1 && last == 8) | |
{ | |
Console.WriteLine("Very special Fibonacci"); | |
} | |
// Slice pattern | |
Console.WriteLine(numbers switch { | |
{ 1, .., var sl, 8 } => $"Starts with 1, ends with 8, and 2nd last number is {sl}", | |
{ 1, _, _, .. } => "Starts with 1 and is at least 3 long", | |
{ 1, .. } => "Starts with 1 and is at least 1 long", | |
_ => "WAT?" | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment