Solving FizzBuzz with C#s Pattern Matching Switch Statements @WorldOfZeroDevelopment
Solving FizzBuzz with C#s Pattern Matching Switch Statements  @WorldOfZeroDevelopment
Uploaded March 2020 | Updated November 2026, 3 hours ago
Pattern Matching was introduced into C# a few years ago and I've not really been able to integrate it into my projects. I wanted to explore the feature a bit more by solving Fizz Buzz. I figured we could learn about it together. 😃

FizzBuzz is a common interview question that covers order of operations, conditionals and loops in the language of your choice. In this video, we'll be replacing the more commonly used `if` statements with a pattern matching switch expression.

The rules of FizzBuzz are:

Iterate over a series of integers.

* If the number is divisible by 3 print "Fizz"
* If the number is divisible by 5 print "Buzz"
* If the number is divisible by 3 **and** 5 print "FizzBuzz"
* Otherwise print the number to the console

For example if we provide 9 we should print "Fizz because 9 can be evenly divided by 3 with no remainder. If we provide 10 we should print "Buzz" because 10 is divisible by 5. If we provide 15 we should print "FizzBuzz" because it is divisible by both 3 and 5.

This is the code from the video (The greater-than and less-than symbols may need to be replaced. YouTube does not support them natively.)

```
class Program
{
static void Main(string[] args)
{
for(int i = 0; i Ë‚ 20; ++i) {
Console.WriteLine(GetOutputString(i));
}
}

static string GetOutputString(int i) {
return (i%3, i%5) switch
{
(0, 0) =˃ "FizzBuzz",
(0, _) =˃ "Fizz",
(_, 0) =˃ "Buzz",
_ =˃ i.ToString()
};
}
}
```

Here are some additional examples and tutorials on using pattern-matching in C#: docs.microsoft.com/en-us/dotnet/csharp/tutorials/pattern-matching

Join the World of Zero Discord Server: discord.gg/hU5Kq2u
Solving FizzBuzz with C#s Pattern Matching Switch StatementsUsing Git HooksLearning to Compile a Java Class From ScratchHow I Use NDepend to Manage my Technical DebtSending Real-Time Messages to Unity from a Blazor Website - SignalR in Unity - Part 3Dynamic Game Speed - Unity Quick TipFiring Projectiles - Lets Make a Side Scrolling Shooter - Part 5I Learn How To Build Flowcharts and Signal Diagrams in Mermaid.JSCollaborative Coding with Visual Studio Live ShareRigidbody Top-Down Spaceship ControllerBuilding Coyote Time in a 2D PlatformerExploring Dot Products in Unity
World of Zero |

Solving FizzBuzz with C#'s Pattern Matching Switch Statements

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER