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

Object type as universal base in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object type as universal base
Create variable of type object
Assign any value (int, string, etc.)
Use variable as universal container
Cast back to original type when needed
Use value
In C#, the object type can hold any data type because all types inherit from it. You store any value in an object variable and cast it back when needed.
Execution Sample
C Sharp (C#)
object obj = 42;
Console.WriteLine(obj);
obj = "hello";
Console.WriteLine(obj);
int num = (int)obj; // causes error
This code stores an int and then a string in an object variable, then tries to cast back to int (which fails).
Execution Table
StepCode LineVariableValueActionNotes
1object obj = 42;obj42 (int)Assign int 42 to objBoxing int into object
2Console.WriteLine(obj);obj42 (int)Print objOutputs 42
3obj = "hello";obj"hello" (string)Assign string to objobj now holds string
4Console.WriteLine(obj);obj"hello" (string)Print objOutputs hello
5int num = (int)obj;obj"hello" (string)Cast obj to intInvalid cast exception at runtime
💡 Execution stops at step 5 due to invalid cast from string to int.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5 (error)
objnull42 (int)"hello" (string)Exception thrown, no value
numundefinedundefinedundefinedundefined (not assigned due to error)
Key Moments - 2 Insights
Why does casting obj to int fail at step 5?
Because obj currently holds a string "hello", not an int. Casting requires the actual stored type to match, as shown in execution_table row 5.
Can obj hold any type of value?
Yes, obj is of type object, the universal base type in C#. It can hold any value type or reference type, as shown in steps 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What type of value does obj hold?
Aint 42
Bobject null
Cstring "hello"
Dint 0
💡 Hint
Check the 'Value' column in execution_table row 3.
At which step does the program throw an error?
AStep 2
BStep 5
CStep 4
DStep 1
💡 Hint
Look at the 'Notes' column for the step where casting fails.
If we assign obj = 100; then cast to int, what would happen?
ACasting succeeds and num = 100
BCasting fails with exception
Cnum becomes null
DCompilation error
💡 Hint
Refer to how boxing/unboxing works in variable_tracker and execution_table step 1.
Concept Snapshot
object is the universal base type in C#
It can hold any value or reference type
Assigning value types boxes them into object
Casting back requires correct original type
Invalid cast causes runtime exception
Use object to store mixed types carefully
Full Transcript
In C#, the object type is the universal base type. This means any value or reference type can be stored in a variable of type object. When you assign a value type like int to an object, it is boxed, meaning wrapped inside an object container. You can then assign a different type like string to the same object variable. However, when you want to get the original value back, you must cast the object to the correct type. If you cast to the wrong type, like casting a string to int, the program throws an invalid cast exception at runtime. This example shows storing 42 (int) in obj, printing it, then storing "hello" (string), printing it, and finally trying to cast back to int which fails. This teaches that object is flexible but requires careful casting.