Skip to content

Instantly share code, notes, and snippets.

@buvinghausen
Created November 19, 2020 15:30
Show Gist options
  • Save buvinghausen/95f30490ed5adff510a02f108703d871 to your computer and use it in GitHub Desktop.
Save buvinghausen/95f30490ed5adff510a02f108703d871 to your computer and use it in GitHub Desktop.
FizzBuzz using C# pattern matching, value tuples, and a switch expression
using System;
using System.Collections.Generic;
using System.Linq;
static List<string> FizzBuzz(int count) => Enumerable
.Range(1, count)
.Select(i => (i % 3 == 0, i % 5 == 0) switch
{
(true, false) => "Fizz",
(false, true) => "Buzz",
(true, true) => "FizzBuzz",
_ => $"{i}"
})
.ToList();
FizzBuzz(63).ForEach(Console.WriteLine);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment