Created
October 17, 2017 11:03
-
-
Save DavidArno/f6e2bce9230e4978b29cd4ec07708129 to your computer and use it in GitHub Desktop.
Fizzbuzz in C# using tuples, pattern matching and local functions
This file contains 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; | |
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