What is the purpose of getting type information at runtime in C#?
It helps you find out what kind of object you have while the program is running. This is useful when you don't know the type in advance and want to make decisions based on the object's type.
Click to reveal answer
beginner
Which C# keyword or method returns the runtime type of an object?
The GetType() method returns the exact type of an object at runtime.
Click to reveal answer
intermediate
How do you compare the runtime type of an object to a known type in C#?
Use obj.GetType() == typeof(SomeType) to check if the object's runtime type matches SomeType.
Click to reveal answer
intermediate
What is the difference between GetType() and typeof() in C#?
GetType() is called on an object instance to get its runtime type. typeof() is used with a type name to get the Type object representing that type at compile time.
Click to reveal answer
beginner
How can you get the name of an object's type at runtime?
Call obj.GetType().Name to get the simple name of the object's type as a string.
Click to reveal answer
Which method do you use to get the runtime type of an object in C#?
ATypeOfObject()
Btypeof()
CGetType()
DGetRuntimeType()
✗ Incorrect
GetType() is called on an object instance to get its runtime type.
What does typeof(int) return in C#?
AThe Type object representing the int type
BThe runtime type of an int variable
CThe string "int"
DAn instance of int
✗ Incorrect
typeof(int) returns the Type object representing the int type at compile time.
How do you check if an object is exactly of type string at runtime?
Aobj is string
Bobj.GetType() == typeof(string)
Cobj.GetType().Name == "string"
Dobj == string
✗ Incorrect
Use obj.GetType() == typeof(string) to check exact type equality.
Which property gives you the simple name of a type in C#?
AType.FullName
BType.ToString()
CType.SimpleName
DType.Name
✗ Incorrect
Type.Name returns the simple name of the type as a string.
If you want to get the full namespace and name of a type, which property do you use?
AType.FullName
BType.Namespace
CType.Name
DType.ToString()
✗ Incorrect
Type.FullName returns the full namespace and type name.
Explain how to get and compare the runtime type of an object in C#.
Think about how you find out an object's type and then check if it matches a known type.
You got /4 concepts.
Describe the difference between GetType() and typeof() in C#.
One works on objects, the other works on type names.
You got /4 concepts.
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
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.
Step 2: Differentiate from other actions
It does not change, create, or delete objects; it only returns type information.
Final Answer:
Returns the exact runtime type of an object -> Option C
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
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().
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.
Final Answer:
obj.GetType() -> Option A
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
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#.
Step 2: Understand GetType().Name output
Calling val.GetType().Name returns the name of the actual runtime type, which is Int32.
Final Answer:
Int32 -> Option A
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
Step 1: Analyze the type comparison
The code compares data.GetType() to string, but string is a keyword, not a Type object.
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).
Final Answer:
Comparing GetType() to a type name instead of a Type object -> Option B
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?