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

Type checking patterns in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a type checking pattern in C#?
A type checking pattern is a way to test if an object is of a certain type and, if so, to extract it into a variable in a single step using the is keyword.
Click to reveal answer
beginner
How do you use the is keyword with a pattern to check type and assign a variable?
You write if (obj is TypeName variableName). This checks if obj is of type TypeName and if true, assigns it to variableName.
Click to reveal answer
intermediate
What is the benefit of using type checking patterns over traditional casting?
Type checking patterns combine the type check and variable assignment in one step, making code safer and cleaner by avoiding explicit casts and potential exceptions.
Click to reveal answer
intermediate
Explain the difference between is TypeName variable and as TypeName casting.
is TypeName variable checks type and assigns if true, while as TypeName tries to cast and returns null if it fails. The is pattern is safer and more concise.
Click to reveal answer
beginner
Show a simple example of type checking pattern in C#.
Example:
object obj = "hello";
if (obj is string s)
{
Console.WriteLine($"String length: {s.Length}");
}
Click to reveal answer
What does the C# expression if (obj is string s) do?
ACasts obj to string without checking
BChecks if obj is a string and assigns it to s if true
CAlways assigns obj to s regardless of type
DThrows an exception if obj is not a string
Which keyword is used in C# for type checking patterns?
Acast
Bas
Cis
Dtypeof
What happens if the type check fails in if (obj is int i)?
AThe if block is skipped and i is not assigned
BAn exception is thrown
Ci is assigned null
DThe program crashes
Which is safer to avoid exceptions when checking type and using the object?
AType checking pattern with 'is'
BDirect casting with (Type)
CUsing 'as' without null check
DIgnoring type and using object directly
What is the main advantage of type checking patterns?
AReplace all if statements
BMake code run faster
CAllow casting to any type without error
DCombine type check and assignment in one step
Explain how type checking patterns work in C# and why they are useful.
Think about how you check type and get the object ready to use in one step.
You got /5 concepts.
    Write a simple C# code snippet using a type checking pattern to check if an object is a string and print its length.
    Use 'if (obj is string s)' and then print s.Length.
    You got /5 concepts.

      Practice

      (1/5)
      1. What does the C# pattern if (obj is string s) do?
      easy
      A. Checks if obj is null
      B. Converts obj to 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

      1. Step 1: Understand the is pattern

        The is keyword checks if an object is of a certain type.
      2. Step 2: Assign variable if type matches

        If obj is a string, it assigns the value to s for use inside the block.
      3. Final Answer:

        Checks if obj is a string and assigns it to s if true -> Option C
      4. Quick Check:

        is Type var checks type and assigns [OK]
      Hint: Remember: is Type var checks and assigns together [OK]
      Common Mistakes:
      • Thinking it converts without checking
      • Assuming it checks for null only
      • Believing it throws exceptions automatically
      2. Which of the following is the correct syntax to check if obj is an int and assign it to number?
      easy
      A. if (obj is int number) { }
      B. if (obj as int number) { }
      C. if (obj == int number) { }
      D. if (obj instanceof int number) { }

      Solution

      1. Step 1: Identify correct C# type pattern syntax

        The correct syntax uses is Type variable to check and assign.
      2. Step 2: Compare options

        if (obj is int number) { } uses is int number, which is valid. Others use invalid keywords or syntax.
      3. Final Answer:

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

        is Type var syntax is correct [OK]
      Hint: Use is Type var for type check and assignment [OK]
      Common Mistakes:
      • Using 'as' instead of 'is' for type checking
      • Using '==' to compare types
      • Using JavaScript or Java keywords like 'instanceof'
      3. What is the output of this code?
      object obj = 42;
      if (obj is int n)
      {
          Console.WriteLine(n + 10);
      }
      else
      {
          Console.WriteLine("Not an int");
      }
      medium
      A. Not an int
      B. 52
      C. 42
      D. Compilation error

      Solution

      1. Step 1: Check the type of obj

        obj holds the integer 42, so obj is int n is true and assigns 42 to n.
      2. Step 2: Calculate the output inside the if block

        It prints n + 10 which is 42 + 10 = 52.
      3. Final Answer:

        52 -> Option B
      4. Quick Check:

        Type check passes, output is 42 + 10 = 52 [OK]
      Hint: If type matches, variable holds value for use [OK]
      Common Mistakes:
      • Printing the original obj instead of n + 10
      • Choosing else output wrongly
      • Thinking it causes a compile error
      4. Identify the error in this code snippet:
      object obj = "hello";
      if (obj is int number)
      {
          Console.WriteLine(number);
      }
      medium
      A. No error, code runs and prints number
      B. Runtime exception thrown
      C. Syntax error in the if statement
      D. The variable 'number' is not assigned because obj is not int

      Solution

      1. Step 1: Analyze the type check

        obj is a string, so obj is int number is false and number is not assigned.
      2. Step 2: Understand the effect on code execution

        The if block is skipped, so nothing prints. No error or exception occurs.
      3. Final Answer:

        The variable 'number' is not assigned because obj is not int -> Option D
      4. Quick Check:

        Type check false means variable not assigned [OK]
      Hint: If type check fails, variable is not assigned [OK]
      Common Mistakes:
      • Thinking it causes syntax error
      • Expecting runtime exception
      • Assuming variable is assigned anyway
      5. Given a list of objects List<object> items = new() { 1, "two", 3, null, 4.5 };, which code snippet correctly sums only the integer values using type checking patterns?
      hard
      A. int sum = 0; foreach (var item in items) { if (item is int n) sum += n; } Console.WriteLine(sum);
      B. int sum = 0; foreach (int n in items) { sum += n; } Console.WriteLine(sum);
      C. int sum = 0; foreach (var item in items) { if (item is double d) sum += (int)d; } Console.WriteLine(sum);
      D. int sum = 0; foreach (var item in items) { sum += (int)item; } Console.WriteLine(sum);

      Solution

      1. Step 1: Understand the list contents

        The list has integers, a string, null, and a double. We want to sum only integers.
      2. Step 2: Check each option for correct type filtering

        int sum = 0; foreach (var item in items) { if (item is int n) sum += n; } Console.WriteLine(sum); uses if (item is int n) to add only integers. int sum = 0; foreach (int n in items) { sum += n; } Console.WriteLine(sum); tries to cast all items to int in foreach, causing error. int sum = 0; foreach (var item in items) { if (item is double d) sum += (int)d; } Console.WriteLine(sum); sums doubles cast to int, which is incorrect. int sum = 0; foreach (var item in items) { sum += (int)item; } Console.WriteLine(sum); casts all items to int without checking, causing runtime errors.
      3. Final Answer:

        int sum = 0; foreach (var item in items) { if (item is int n) sum += n; } Console.WriteLine(sum); -> Option A
      4. Quick Check:

        Use is int var to filter integers safely [OK]
      Hint: Use if (item is int n) to sum integers safely [OK]
      Common Mistakes:
      • Casting without checking causing exceptions
      • Summing wrong types like double or string
      • Assuming foreach int works on object list