A type pattern checks if an object is of a specific type and, if so, allows you to use it as that type in a safe way.
Click to reveal answer
beginner
How do you use a type pattern with the is keyword?
You write if (obj is TypeName variableName). This checks if obj is of TypeName and, if true, assigns it to variableName for use inside the block.
Click to reveal answer
beginner
What happens if the type pattern check fails?
If the object is not of the specified type, the pattern check returns false and the variable is not assigned.
Click to reveal answer
intermediate
Can you use type patterns in a switch statement?
Yes! You can match the type of a variable in switch cases using type patterns, making code clearer and safer.
Click to reveal answer
beginner
Example: What does this code do?
if (obj is string s) { Console.WriteLine(s.Length); }
It checks if obj is a string. If yes, it assigns it to s and prints the length of the string.
Click to reveal answer
What does the type pattern if (obj is int number) do?
AChecks if obj is an int and assigns it to number if true
BConverts obj to int without checking
CAlways assigns obj to number
DChecks if obj is null
✗ Incorrect
The pattern checks the type and assigns only if obj is an int.
Which keyword is used with type patterns to check an object's type?
Ais
Btypeof
Cswitch
Das
✗ Incorrect
The 'is' keyword is used to check type patterns.
What happens if the type pattern check fails?
AThe variable is assigned default value
BThe code throws an exception
CThe variable is assigned null
DThe condition is false and variable is not assigned
✗ Incorrect
If the object is not of the specified type, the check returns false and the variable is not assigned.
Can type patterns be used inside a switch statement?
ANo, only if statements
BYes, to match types in cases
COnly with enums
DOnly with strings
✗ Incorrect
Type patterns can be used in switch cases to match object types.
What is the benefit of using type patterns?
AThey automatically convert types
BThey replace all casts
CThey allow safe type checking and variable assignment
DThey improve performance by skipping checks
✗ Incorrect
Type patterns let you check types safely and use the variable if the check passes.
Explain how type patterns work with the 'is' keyword in C#.
Think about how you check and use an object's type in one step.
You got /3 concepts.
Describe how type patterns can be used in a switch statement and why that is useful.
Consider how switch can handle different types safely.
You got /3 concepts.
Practice
(1/5)
1. What does a type pattern in C# do when used in an if statement like if (obj is string s)?
easy
A. Always returns true regardless of obj type
B. Converts obj to a string without checking
C. Checks if obj is a string and assigns it to s if true
D. Throws an exception if obj is not a string
Solution
Step 1: Understand the is keyword with type pattern
The is keyword checks if the object is of the specified type.
Step 2: Recognize the pattern variable assignment
If the check is true, the object is assigned to the variable s of that type.
Final Answer:
Checks if obj is a string and assigns it to s if true -> Option C
Quick Check:
Type pattern = Check and assign [OK]
Hint: Type pattern checks type and assigns in one step [OK]
Common Mistakes:
Thinking it converts without checking
Assuming it always returns true
Expecting an exception on type mismatch
2. Which of the following is the correct syntax for using a type pattern in an if statement in C#?
easy
A. if (obj as int number) { }
B. if (int obj is number) { }
C. if (obj int is number) { }
D. if (obj is int number) { }
Solution
Step 1: Recall correct type pattern syntax
The correct syntax is if (variable is Type name).
Step 2: Check each option
if (obj is int number) { } matches the correct syntax. Options B, C, and D have incorrect order or keywords.
Final Answer:
if (obj is int number) { } -> Option D
Quick Check:
Correct syntax = if (obj is Type name) [OK]
Hint: Remember: if (obj is Type var) [OK]
Common Mistakes:
Swapping type and variable order
Using 'as' instead of 'is' for pattern
Placing keywords incorrectly
3. What will be the output of this C# code?
object obj = 42; if (obj is int number) { Console.WriteLine(number + 10); } else { Console.WriteLine("Not an int"); }
medium
A. 52
B. Not an int
C. Compilation error
D. Runtime exception
Solution
Step 1: Check the type of obj
obj holds the integer 42, so obj is int is true.
Step 2: Calculate the output inside the if block
The variable number is assigned 42, so number + 10 equals 52, which is printed.
Final Answer:
52 -> Option A
Quick Check:
Type check true, output = 42 + 10 = 52 [OK]
Hint: If type matches, variable holds value for use [OK]
Common Mistakes:
Expecting 'Not an int' output
Confusing type check with cast
Thinking code throws error
4. Identify the error in this code snippet using type patterns:
object obj = "hello"; if (obj is int number) { Console.WriteLine(number); }
medium
A. No output because obj is not an int
B. Compilation error due to missing else block
C. No error, code runs and prints 'hello'
D. Runtime exception when casting string to int
Solution
Step 1: Analyze the type check condition
obj is a string, so obj is int number is false.
Step 2: Understand the effect on output
Since the if condition is false and no else block exists, nothing is printed.
Final Answer:
No output because obj is not an int -> Option A
Quick Check:
False type check = no output [OK]
Hint: False type check skips block, no output [OK]
Common Mistakes:
Expecting runtime exception
Thinking else block is mandatory
Assuming it prints the string
5. Given this method using type patterns, what will it return? <pre>string Describe(object input) { return input switch { int i => $"Integer: {i}", string s => $"String: {s}", null => "Null value", _ => "Unknown type" }; }
What is the result of Describe(3.14)?
hard
A. "Integer: 3"
B. "Unknown type"
C. "String: 3.14"
D. Compilation error
Solution
Step 1: Check the switch cases for input 3.14
3.14 is a double, which does not match int, string, or null.
Step 2: Identify the default case
The underscore _ matches any other type, so it returns "Unknown type".