Skip to content

Instantly share code, notes, and snippets.

@DavidArno
Created October 17, 2017 11:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidArno/f6e2bce9230e4978b29cd4ec07708129 to your computer and use it in GitHub Desktop.
Save DavidArno/f6e2bce9230e4978b29cd4ec07708129 to your computer and use it in GitHub Desktop.
Fizzbuzz in C# using tuples, pattern matching and local functions
using System;
namespace FizzBuzz
{
static class Program
{
static void Main()
{
for (var i = 1; i <= 100; i++)
{
Console.WriteLine(FizzBuzz(i));
}
string FizzBuzz(int value) => (value % 3, value % 5) is var t && t.Item1 == 0 || t.Item2 == 0
? $"{NameIfZero(t.Item1, "Fizz")}{NameIfZero(t.Item2, "Buzz")}"
: value.ToString();
string NameIfZero(int value, string name) => value == 0 ? name : "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment