0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 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.