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

Share this Doc

Pattern Matching in switch (C# 9.0+)

Or copy link