0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 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.