Getting type information at runtime in C Sharp (C#) - Time & Space Complexity
When we get type information at runtime, the program checks details about an object while it runs.
We want to know how the time needed grows as the program asks about more objects.
Analyze the time complexity of the following code snippet.
object[] items = new object[n];
for (int i = 0; i < n; i++)
{
Type t = items[i].GetType();
Console.WriteLine(t.Name);
}
This code loops through an array of objects and prints each object's type name.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling
GetType()on each object in the array. - How many times: Once for each of the
nobjects in the array.
As the number of objects grows, the program checks each one's type once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to GetType() |
| 100 | 100 calls to GetType() |
| 1000 | 1000 calls to GetType() |
Pattern observation: The number of operations grows directly with the number of objects.
Time Complexity: O(n)
This means the time needed grows in a straight line as the number of objects increases.
[X] Wrong: "Getting type information is instant and does not depend on the number of objects."
[OK] Correct: Each object's type must be checked separately, so more objects mean more work.
Understanding how runtime type checks scale helps you write efficient code and explain your reasoning clearly.
"What if we cached the type information for each object? How would the time complexity change?"
Practice
GetType() method do in C#?Solution
Step 1: Understand the purpose of GetType()
TheGetType()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 CQuick Check:
GetType() returns type info [OK]
- Thinking GetType() changes the object
- Confusing GetType() with creating objects
- Assuming GetType() deletes objects
obj in C#?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 becausetypeofexpects a type name, not a variable, andobj.Type()does not exist.Final Answer:
obj.GetType() -> Option AQuick Check:
Call GetType() on the object [OK]
- Using typeof with a variable instead of a type
- Trying to call GetType as a standalone function
- Using incorrect method names like Type()
object val = 123; Console.WriteLine(val.GetType().Name);
Solution
Step 1: Identify the runtime type of val
The variablevalis declared asobjectbut holds the integer value 123, which is of typeInt32in C#.Step 2: Understand GetType().Name output
Callingval.GetType().Namereturns the name of the actual runtime type, which isInt32.Final Answer:
Int32 -> Option AQuick Check:
Runtime type name = Int32 [OK]
- Expecting 'int' instead of 'Int32'
- Thinking it returns 'Object' because of variable type
- Confusing with string output
object data = "hello";
if (data.GetType() == string)
{
Console.WriteLine("It's a string!");
}Solution
Step 1: Analyze the type comparison
The code comparesdata.GetType()tostring, butstringis a keyword, not a Type object.Step 2: Correct way to compare types
To compare types, usetypeof(string)which returns the Type object for string. So the condition should bedata.GetType() == typeof(string).Final Answer:
Comparing GetType() to a type name instead of a Type object -> Option BQuick Check:
Use typeof(TypeName) for type comparison [OK]
- Using type name without typeof()
- Thinking == can't compare types
- Assuming GetType() is invalid on object
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");
}Solution
Step 1: Understand the method logic
The method usesobj.GetType()to get the runtime type and compares it withtypeof(int)andtypeof(string)to identify the type.Step 2: Verify correctness of type checks
This is a valid and common way to check exact types at runtime in C#. UsingGetType()withtypeof()is correct and safe.Final Answer:
This code correctly checks types using GetType() and typeof() -> Option DQuick Check:
GetType() + typeof() = correct type check [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
