Skip to content

Instantly share code, notes, and snippets.

@rstropek
Created June 6, 2021 13:57
Show Gist options
  • Save rstropek/377d6e82b60a415270553ab1e9a4e7a1 to your computer and use it in GitHub Desktop.
Save rstropek/377d6e82b60a415270553ab1e9a4e7a1 to your computer and use it in GitHub Desktop.
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