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

Getting type information at runtime in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Getting type information at runtime
Start Program
↓
Create Object
↓
Call GetType()
↓
Retrieve Type Info
↓
Use Type Info (e.g., Name)
↓
Display or Use Result
↓
End Program
The program creates an object, calls GetType() to get its type info, then uses that info to display or decide something.
Execution Sample
C Sharp (C#)
string name = "Hello";
Type t = name.GetType();
Console.WriteLine(t.Name);
This code gets the type of the string variable and prints its type name.
Execution Table
StepActionVariable/ExpressionResult/ValueOutput
1Create string variablename"Hello"
2Call GetType() on namename.GetType()System.String (Type object)
3Assign Type object to ttSystem.String (Type object)
4Access t.Name propertyt.Name"String"
5Print t.NameConsole.WriteLine(t.Name)String
💡 Program ends after printing the type name of the variable.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
namenull"Hello""Hello""Hello""Hello""Hello"
tnullnullSystem.String (Type object)System.String (Type object)System.String (Type object)System.String (Type object)
t.Namenullnullnull"String""String""String"
Key Moments - 3 Insights
Why do we call GetType() on the variable instead of the type name directly?
GetType() is called on the object instance (see Step 2) to get its runtime type. Calling it on the type name itself is not valid because GetType() is an instance method.
What does the Type object represent?
The Type object (assigned to t in Step 3) holds metadata about the variable's type, like its name, methods, properties, etc.
Why does Console.WriteLine(t.Name) print "String"?
Because t.Name accesses the Name property of the Type object, which returns the simple name of the type, here "String" (Step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of variable 't' after Step 3?
A"Hello"
Bnull
CSystem.String (Type object)
D"String"
💡 Hint
Check the 'Result/Value' column for Step 3 in the execution_table.
At which step does the program output the type name?
AStep 5
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table to find when output occurs.
If we changed 'name' to an integer variable, what would t.Name be after Step 4?
A"String"
B"Int32"
C"Object"
D"Integer"
💡 Hint
The Type.Name returns the simple type name of the variable's runtime type (see variable_tracker for t.Name).
Concept Snapshot
Use GetType() on an object to get its runtime type info.
The returned Type object has properties like Name to identify the type.
Example: string s = "Hi"; Type t = s.GetType(); t.Name is "String".
GetType() is an instance method, so call it on variables, not type names.
Useful for checking or displaying type info during program execution.
Full Transcript
This example shows how to get type information at runtime in C#. We start by creating a string variable named 'name' with the value "Hello". Then we call the GetType() method on 'name' to get its type information, which returns a Type object representing System.String. We store this Type object in the variable 't'. Next, we access the Name property of 't' to get the simple name of the type, which is "String". Finally, we print this name to the console. This process helps us find out what type a variable is while the program is running.

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