Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Why pattern matching matters in C Sharp (C#) - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the object is an integer using pattern matching.

C Sharp (C#)
if (obj is [1] number) {
    Console.WriteLine($"Number is {number}");
}
Drag options to blanks, or click blank then click option'
Abool
Bint
Cdouble
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' instead of 'int' for integer check.
2fill in blank
medium

Complete the code to use pattern matching in a switch expression to return a message based on the shape type.

C Sharp (C#)
string message = shape switch {
    Circle c => $"Circle with radius {c.Radius}",
    Rectangle r => $"Rectangle {r.Width}x{r.Height}",
    [1] => "Unknown shape"
};
Drag options to blanks, or click blank then click option'
Adefault
Bnull
C_
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'default' keyword which is invalid in this context.
3fill in blank
hard

Fix the error in the pattern matching expression to correctly check if the object is a string with length greater than 5.

C Sharp (C#)
if (obj is string s && s.Length [1] 5) {
    Console.WriteLine("Long string");
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '==' which do not check for longer strings.
4fill in blank
hard

Fill both blanks to create a pattern matching expression that checks if the object is a Point with X greater than 0 and Y less than 0.

C Sharp (C#)
if (obj is Point [1] && [1].X > 0 && [1].Y [2] 0) {
    Console.WriteLine("Point in quadrant 4");
}
Drag options to blanks, or click blank then click option'
Ap
B>
C<
Dq
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'q' instead of 'p' for the variable name.
Using '>' instead of '<' for Y comparison.
5fill in blank
hard

Fill all three blanks to create a switch expression that returns the area of shapes, using pattern matching with property patterns.

C Sharp (C#)
double area = shape switch {
    Circle [1] => Math.PI * [2].Radius * [3].Radius,
    Rectangle r => r.Width * r.Height,
    _ => 0
};
Drag options to blanks, or click blank then click option'
Ac
Dr
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using 'r' which is for Rectangle, not Circle.

Practice

(1/5)
1. What is the main benefit of using pattern matching in C#?
easy
A. It simplifies checking types and extracting data in one step.
B. It makes the program run faster by optimizing CPU usage.
C. It automatically fixes syntax errors in the code.
D. It replaces all loops with recursive calls.

Solution

  1. Step 1: Understand pattern matching purpose

    Pattern matching lets you check a variable's type and extract its data in a single, clear step.
  2. Step 2: Compare with other options

    Options A, B, and D describe unrelated or incorrect benefits not provided by pattern matching.
  3. Final Answer:

    It simplifies checking types and extracting data in one step. -> Option A
  4. Quick Check:

    Pattern matching = Simplifies type checks [OK]
Hint: Pattern matching combines type check and data use in one step [OK]
Common Mistakes:
  • Thinking pattern matching speeds up CPU performance
  • Confusing pattern matching with syntax error fixing
  • Believing pattern matching replaces loops
2. Which of the following is the correct syntax for pattern matching with is in C#?
easy
A. if (obj is int number) { /* use number */ }
B. if (obj is int) number { /* use number */ }
C. if (obj is int: number) { /* use number */ }
D. if (obj int is number) { /* use number */ }

Solution

  1. Step 1: Recall correct pattern matching syntax

    The correct syntax uses is Type variable inside the if condition, like obj is int number.
  2. Step 2: Identify syntax errors in other options

    Options A, B, and C have incorrect order or punctuation that cause syntax errors.
  3. Final Answer:

    if (obj is int number) { /* use number */ } -> Option A
  4. Quick Check:

    Correct pattern matching syntax = if (obj is int number) { /* use number */ } [OK]
Hint: Remember: 'is Type variable' is the pattern matching syntax [OK]
Common Mistakes:
  • Placing variable before type in 'is' expression
  • Using colon ':' instead of space
  • Mixing order of keywords
3. What will be the output of this code?
object obj = 42;
switch (obj)
{
    case int i:
        Console.WriteLine($"Integer: {i}");
        break;
    case string s:
        Console.WriteLine($"String: {s}");
        break;
    default:
        Console.WriteLine("Unknown type");
        break;
}
medium
A. String: 42
B. Integer: 42
C. Unknown type
D. Compilation error

Solution

  1. Step 1: Identify the type of obj

    obj is assigned the integer value 42, so its runtime type is int.
  2. Step 2: Match switch cases

    The switch matches case int i because obj is an int, so it prints "Integer: 42".
  3. Final Answer:

    Integer: 42 -> Option B
  4. Quick Check:

    obj is int 42, so output = "Integer: 42" [OK]
Hint: Match the runtime type to the correct case in switch [OK]
Common Mistakes:
  • Assuming obj is string because of quotes missing
  • Thinking default case runs first
  • Confusing compile-time and runtime types
4. Identify the error in this pattern matching code:
object obj = "hello";
if (obj is int number)
{
    Console.WriteLine(number);
}
medium
A. The variable 'number' is not declared before use.
B. The 'is' keyword cannot be used with variables.
C. Missing braces around the if block.
D. The pattern matching condition will never be true for a string.

Solution

  1. Step 1: Analyze the type of obj

    obj holds a string "hello", but the pattern checks if obj is an int.
  2. Step 2: Understand pattern matching behavior

    Since obj is a string, the condition obj is int number is false, so the block never runs.
  3. Final Answer:

    The pattern matching condition will never be true for a string. -> Option D
  4. Quick Check:

    obj is string, pattern checks int, so condition false [OK]
Hint: Check if variable type matches pattern type before using it [OK]
Common Mistakes:
  • Assuming variable 'number' must be declared separately
  • Thinking 'is' cannot be used with variables
  • Ignoring that condition can be false without error
5. Given a list of objects List<object> items = new() { 1, -2, "two", 3.0, null };, which pattern matching expression correctly counts only the positive integer values?
hard
A. items.Count(item => item is string s)
B. items.Count(item => item is int)
C. items.Count(item => item is int number && number > 0)
D. items.Count(item => item != null)

Solution

  1. Step 1: Understand the goal

    We want to count only positive integers in the list, ignoring other types and null.
  2. Step 2: Analyze each option

    items.Count(item => item is int) counts all ints but does not filter by value; items.Count(item => item is int number && number > 0) counts ints greater than 0, which matches positive integers; items.Count(item => item is string s) counts strings; items.Count(item => item != null) counts all non-null items.
  3. Step 3: Choose the best option

    items.Count(item => item is int number && number > 0) uses pattern matching with a condition to count only positive integers, which is a precise use of pattern matching.
  4. Final Answer:

    items.Count(item => item is int number && number > 0) -> Option C
  5. Quick Check:

    Pattern matching with condition filters integers correctly [OK]
Hint: Use 'is Type var && condition' to filter precisely [OK]
Common Mistakes:
  • Counting all ints without condition when needed
  • Counting strings or non-null items by mistake
  • Not using variable in pattern matching for conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard