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

Constant patterns in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Constant patterns
Start
↓
Match value with constant pattern
↓
End
The program checks if a value matches a constant pattern. If yes, it runs matched code; if no, it runs other code or skips.
Execution Sample
C Sharp (C#)
int number = 5;
if (number is 5)
{
    Console.WriteLine("Number is five");
}
else
{
    Console.WriteLine("Number is not five");
}
Checks if number equals 5 using a constant pattern and prints a message accordingly.
Execution Table
StepVariable 'number'Condition 'number is 5'Branch TakenOutput
15TrueIf branchNumber is five
25N/AEndProgram ends
💡 Condition is true at step 1, so if branch runs and program ends after.
Variable Tracker
VariableStartAfter Step 1Final
number555
Key Moments - 2 Insights
Why does the code use 'is 5' instead of '== 5'?
The 'is 5' is a constant pattern that checks if the value matches 5 exactly, similar to '== 5'. It is used in pattern matching syntax. See execution_table step 1 where condition is evaluated.
What happens if 'number' is not 5?
If 'number' is not 5, the condition 'number is 5' is false and the else branch runs. This is shown by the 'No' path in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'number' at step 1?
A5
B0
Cnull
D10
💡 Hint
Check the 'Variable number' column at step 1 in execution_table.
At which step does the program decide to print 'Number is five'?
AStep 2
BNo step, it never prints
CStep 1
DBefore step 1
💡 Hint
Look at the 'Branch Taken' and 'Output' columns in execution_table.
If 'number' was 3 instead of 5, what would change in the execution_table?
ACondition would be true and if branch runs
BCondition would be false and else branch runs
CVariable 'number' would be 5
DProgram would crash
💡 Hint
Refer to concept_flow where 'No' branch leads to other code.
Concept Snapshot
Constant patterns check if a value equals a fixed constant.
Syntax: 'variable is constantValue'.
If true, matched code runs.
If false, else or other code runs.
Useful for clear, readable condition checks.
Full Transcript
This example shows how constant patterns work in C#. The program sets a variable 'number' to 5. It then checks if 'number is 5' using a constant pattern. If true, it prints 'Number is five'. Otherwise, it prints 'Number is not five'. The flow starts by checking the condition. Since number is 5, the condition is true and the if branch runs. The variable 'number' stays 5 throughout. If the number was different, the else branch would run. Constant patterns are a simple way to compare values in pattern matching syntax.

Practice

(1/5)
1. What does a constant pattern in C# do when used in a switch statement?
easy
A. It checks if the value is greater than a constant.
B. It checks if the value exactly matches a fixed constant.
C. It checks if the value is less than a constant.
D. It checks if the value is of a certain type.

Solution

  1. Step 1: Understand constant pattern purpose

    Constant patterns compare a value to a fixed constant to see if they are equal.
  2. Step 2: Apply to switch statement

    In a switch, constant patterns match cases where the value equals the constant exactly.
  3. Final Answer:

    It checks if the value exactly matches a fixed constant. -> Option B
  4. Quick Check:

    Constant pattern = exact value check [OK]
Hint: Constant pattern means exact value match check [OK]
Common Mistakes:
  • Thinking it checks ranges instead of exact values
  • Confusing with type patterns
  • Assuming it checks inequality
2. Which of the following is the correct syntax for a constant pattern in an if statement in C#?
easy
A. if (is x 10) { }
B. if (x == is 10) { }
C. if (x is 10) { }
D. if (x = 10) { }

Solution

  1. Step 1: Recall constant pattern syntax

    The syntax uses is followed by the constant value to check equality.
  2. Step 2: Check each option

    if (x is 10) { } uses if (x is 10), which is correct. Others have syntax errors.
  3. Final Answer:

    if (x is 10) { } -> Option C
  4. Quick Check:

    Correct constant pattern syntax = if (x is 10) [OK]
Hint: Use 'is' keyword followed by constant value [OK]
Common Mistakes:
  • Using assignment '=' instead of 'is'
  • Placing 'is' incorrectly
  • Adding extra operators
3. What will be the output of the following C# code?
int number = 5;
switch (number)
{
    case 3:
        Console.WriteLine("Three");
        break;
    case 5:
        Console.WriteLine("Five");
        break;
    default:
        Console.WriteLine("Other");
        break;
}
medium
A. Five
B. Three
C. Other
D. No output

Solution

  1. Step 1: Identify the value of 'number'

    The variable 'number' is assigned the value 5.
  2. Step 2: Match the switch cases

    The switch checks cases 3 and 5. Since number is 5, it matches case 5 and prints "Five".
  3. Final Answer:

    Five -> Option A
  4. Quick Check:

    number = 5 matches case 5 [OK]
Hint: Match value to case constant exactly [OK]
Common Mistakes:
  • Choosing default instead of matching case
  • Confusing case values
  • Ignoring break statements
4. Identify the error in this C# code using constant patterns:
object obj = 10;
if (obj is 10)
{
    Console.WriteLine("Ten");
}
medium
A. No error, code is correct
B. Missing cast to int before pattern matching
C. Cannot use constant pattern with object type
D. Constant pattern requires a variable after 'is'

Solution

  1. Step 1: Understand constant pattern with object

    Constant patterns can be used with object type to check if it equals a constant value.
  2. Step 2: Check code correctness

    The code uses obj is 10 correctly to check if obj equals 10. This is valid syntax and logic.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    Constant pattern works with object type [OK]
Hint: Constant pattern works directly on object values [OK]
Common Mistakes:
  • Assuming cast needed before pattern
  • Thinking constant pattern needs variable
  • Confusing with type patterns
5. Given a list of integers, which C# code correctly uses constant patterns to count how many times the number 7 appears?
var numbers = new List<int> {1, 7, 3, 7, 5};
int count = 0;
foreach (var n in numbers)
{
    // Fill in the blank
}
Console.WriteLine(count);
hard
A. if (n == 7) count = 7;
B. if (n is not 7) count++;
C. if (n is int) count++;
D. if (n is 7) count++;

Solution

  1. Step 1: Understand the goal

    We want to count how many times 7 appears in the list.
  2. Step 2: Use constant pattern to check for 7

    The correct constant pattern is if (n is 7) which checks if n equals 7, then increments count.
  3. Final Answer:

    if (n is 7) count++; -> Option D
  4. Quick Check:

    Constant pattern for 7 increments count [OK]
Hint: Use 'is' with constant to check exact value [OK]
Common Mistakes:
  • Assigning count instead of incrementing
  • Checking type instead of value
  • Counting non-7 values
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