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

Getting type information at runtime in C Sharp (C#) - Interactive Code Practice

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

Complete the code to get the type of the variable number.

C Sharp (C#)
int number = 5;
Type typeInfo = number.[1];
Drag options to blanks, or click blank then click option'
AGetType()
Btypeof()
CGetType
DTypeOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using typeof() on an instance instead of a type.
Forgetting the parentheses after GetType.
2fill in blank
medium

Complete the code to print the name of the type of text.

C Sharp (C#)
string text = "hello";
Console.WriteLine(text.[1].Name);
Drag options to blanks, or click blank then click option'
AGetType
Btypeof()
CGetType()
DTypeName()
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetType without parentheses.
Trying to use typeof() on a variable instead of a type.
3fill in blank
hard

Fix the error in the code to get the type of obj and print its full name.

C Sharp (C#)
object obj = 10;
Type t = [1];
Console.WriteLine(t.FullName);
Drag options to blanks, or click blank then click option'
Aobj.GetType()
BGetType(obj)
Ctypeof(obj)
Dobj.Type()
Attempts:
3 left
💡 Hint
Common Mistakes
Using typeof with a variable instead of a type.
Trying to call a non-existent method Type().
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only include words longer than 3 characters.

C Sharp (C#)
var words = new List<string> { "cat", "house", "tree", "a" };
var lengths = words.ToDictionary(word => word, word => word.[1])
    .Where(kv => kv.Value [2] 3)
    .ToDictionary(kv => kv.Key, kv => kv.Value);
Drag options to blanks, or click blank then click option'
ALength
B>
C<
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the filter.
Using Count instead of Length for string length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values only for entries where the value is greater than 10.

C Sharp (C#)
var data = new Dictionary<string, int> { {"a", 5}, {"b", 15}, {"c", 20} };
var result = data.Where(kv => kv.Value [1] 10)
    .ToDictionary(kv => kv.Key.[2](), kv => kv.[3]);
Drag options to blanks, or click blank then click option'
A<
B>
CValue
DToUpper
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the filter.
Using Key instead of Value for dictionary values.

Practice

(1/5)
1. What does the GetType() method do in C#?
easy
A. Changes the type of an object
B. Creates a new object of a specified type
C. Returns the exact runtime type of an object
D. Deletes the object from memory

Solution

  1. Step 1: Understand the purpose of GetType()

    The GetType() method is used to find out the exact type of an object while the program is running.
  2. Step 2: Differentiate from other actions

    It does not change, create, or delete objects; it only returns type information.
  3. Final Answer:

    Returns the exact runtime type of an object -> Option C
  4. Quick Check:

    GetType() returns type info [OK]
Hint: Remember: GetType() tells you what type an object is [OK]
Common Mistakes:
  • Thinking GetType() changes the object
  • Confusing GetType() with creating objects
  • Assuming GetType() deletes objects
2. Which of the following is the correct syntax to get the type of an object obj in C#?
easy
A. obj.GetType()
B. GetType(obj)
C. typeof(obj)
D. obj.Type()

Solution

  1. Step 1: Recall method call syntax

    In C#, to get the runtime type of an object, you call the method on the object: obj.GetType().
  2. Step 2: Understand other options

    GetType(obj) is invalid syntax, typeof(obj) is incorrect because typeof expects a type name, not a variable, and obj.Type() does not exist.
  3. Final Answer:

    obj.GetType() -> Option A
  4. Quick Check:

    Call GetType() on the object [OK]
Hint: Use dot notation: object.GetType() [OK]
Common Mistakes:
  • Using typeof with a variable instead of a type
  • Trying to call GetType as a standalone function
  • Using incorrect method names like Type()
3. What will be the output of the following code?
object val = 123;
Console.WriteLine(val.GetType().Name);
medium
A. Int32
B. int
C. Object
D. String

Solution

  1. Step 1: Identify the runtime type of val

    The variable val is declared as object but holds the integer value 123, which is of type Int32 in C#.
  2. Step 2: Understand GetType().Name output

    Calling val.GetType().Name returns the name of the actual runtime type, which is Int32.
  3. Final Answer:

    Int32 -> Option A
  4. Quick Check:

    Runtime type name = Int32 [OK]
Hint: GetType().Name shows exact type name [OK]
Common Mistakes:
  • Expecting 'int' instead of 'Int32'
  • Thinking it returns 'Object' because of variable type
  • Confusing with string output
4. Identify the error in this code snippet:
object data = "hello";
if (data.GetType() == string)
{
    Console.WriteLine("It's a string!");
}
medium
A. Missing semicolon after Console.WriteLine
B. Comparing GetType() to a type name instead of a Type object
C. Using == instead of .Equals() for type comparison
D. GetType() cannot be used on object variables

Solution

  1. Step 1: Analyze the type comparison

    The code compares data.GetType() to string, but string is a keyword, not a Type object.
  2. Step 2: Correct way to compare types

    To compare types, use typeof(string) which returns the Type object for string. So the condition should be data.GetType() == typeof(string).
  3. Final Answer:

    Comparing GetType() to a type name instead of a Type object -> Option B
  4. Quick Check:

    Use typeof(TypeName) for type comparison [OK]
Hint: Compare with typeof(TypeName), not just type name [OK]
Common Mistakes:
  • Using type name without typeof()
  • Thinking == can't compare types
  • Assuming GetType() is invalid on object
5. You want to write a method that accepts an object and prints a special message if the object is an integer or a string. Which code correctly uses runtime type information to do this?
void CheckType(object obj)
{
    if (obj.GetType() == typeof(int))
        Console.WriteLine("Integer detected");
    else if (obj.GetType() == typeof(string))
        Console.WriteLine("String detected");
    else
        Console.WriteLine("Other type");
}
hard
A. The code should compare obj.GetType().Name to string literals
B. This code should use obj is int instead of GetType()
C. GetType() cannot be used inside methods
D. This code correctly checks types using GetType() and typeof()

Solution

  1. Step 1: Understand the method logic

    The method uses obj.GetType() to get the runtime type and compares it with typeof(int) and typeof(string) to identify the type.
  2. Step 2: Verify correctness of type checks

    This is a valid and common way to check exact types at runtime in C#. Using GetType() with typeof() is correct and safe.
  3. Final Answer:

    This code correctly checks types using GetType() and typeof() -> Option D
  4. Quick Check:

    GetType() + typeof() = correct type check [OK]
Hint: Use GetType() == typeof(Type) for exact type checks [OK]
Common Mistakes:
  • Thinking GetType() can't be used in methods
  • Using 'is' when exact type match is needed
  • Comparing type names as strings instead of Type objects
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