Complete C# Tutorial

C# Pattern Matching – The Smart Way to Make Decisions 🤖

Hey there, C# learner! 👋 Ever struggled with writing if-else chains or nested switch cases just to check types or conditions? 😵

Well, C# 9.0+ has something cool for you—Pattern Matching! 🎉 It makes your code smarter, shorter, and cleaner.

Imagine you have different types of data (like numbers, strings, objects), and you need to handle them differently. Instead of writing messy if-else statements, Pattern Matching lets you match data directly inside a switch expression! 🚀

Why Use Pattern Matching? 🤔

No need for extra type checks – C# does it for you!
Less code, more readability – No more nested if-else.
Works with numbers, strings, objects, and more!

Syntax of Pattern Matching in C#

Here’s how Pattern Matching works inside a switch statement:

				
					switch (variable)
{
    case Type1 varName1:
        // Code for Type1
        break;

    case Type2 varName2 when (condition):
        // Code for Type2 with condition
        break;

    case Type3:
        // Code for Type3
        break;

    default:
        // Default case (if no match)
        break;
}
				
			

Breaking it Down 🧐

  1. variable → The value being checked.
  2. case Type1 varName1 → Matches if variable is of Type1.
  3. case Type2 varName2 when (condition) → Matches Type2 only if the when condition is true.
  4. case Type3 → Matches specific values or types.
  5. default → If nothing matches, this block executes.

Example: Pattern Matching in Switch Case (Checking Different Types) 🚀

Let’s say you’re building a smart response system that reacts differently based on the type of input.

				
					using System;

class Program
{
    static void Main()
    {
        object input = 42; // Try changing to "Hello" or 3.14

        switch (input)
        {
            case int number when number > 0:
                Console.WriteLine("Positive Number ✅");
                break;

            case int number when number < 0:
                Console.WriteLine("Negative Number ❌");
                break;

            case string text:
                Console.WriteLine($"It's a string: {text} ✍️");
                break;

            case double decimalNumber:
                Console.WriteLine($"It's a decimal number: {decimalNumber} 🔢");
                break;

            default:
                Console.WriteLine("Unknown Type 🤷‍♂️");
                break;
        }
    }
}
				
			

Output Examples (Based on Input)

If input = 42

				
					Positive Number ✅
				
			

If input = "Hello"

				
					It's a string: Hello ✍️
				
			

If input = 3.14

				
					It's a decimal number: 3.14 🔢
				
			

Key Features of Pattern Matching in Switch Case

  1. Checks types dynamically (e.g., int, string, double).
  2. Uses when to add conditions inside cases.
  3. No need for type casting! 🚀
  4. Handles multiple cases smoothly without long if-else chains.

Final Thoughts 💡

Now you know how Pattern Matching in Switch Case makes your code smarter, cleaner, and more powerful! 🎯

👉 Try changing the input value and see how it reacts! Which type will you test next? Let’s have fun coding! 😃🚀

Conclusion 🎯

Pattern Matching in C# saves you time and effort by letting you match types directly inside a switch expression. Instead of writing long if-else statements, you can write cleaner and smarter code! 🚀

Next time you need to check an object’s type, ask yourself: Can I use Pattern Matching instead? If yes, go for it! 😎

Next What? 🚀

Great job! 🎉 You’re getting better and better at writing smart C# code!

In the next lesson, you’ll learn about Switch with When Clause – a powerful way to add extra conditions inside a switch case! 🧐

👉 Excited? Let’s jump to the next lesson! 🚀

Leave a Comment

twenty − 6 =

Share this Doc

Pattern Matching in switch (C# 9.0+)

Or copy link