DEV Community

Discussion on: [Challenge] 🐝 FizzBuzz without if/else

Collapse
anras573 profile image
Anders Bo Rasmussen • Edited on

I did something similar to your fizzbuzz in C# 8.0:

public static void Main()
{       
    for (var i = 1; i < 31; i++)
    {
        Console.WriteLine(FizzBuzz(i));
    }
}

public static string FizzBuzz(int number)
{
    return number switch
    {
        _ when number % 15 == 0 => "FizzBuzz",
        _ when number % 3 == 0 => "Fizz",
        _ when number % 5 == 0 => "Buzz",
        _ => number.ToString()
    };
}
Enter fullscreen mode Exit fullscreen mode