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

Creating instances dynamically in C Sharp (C#) - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating instances dynamically
Start
Get Type info
Call Activator.CreateInstance
New object created
Use the object
End
This flow shows how a program gets type information, creates an instance dynamically, and then uses the new object.
Execution Sample
C Sharp (C#)
using System;

class Person {
  public string Name;
  public Person() { Name = "Unknown"; }
}

class Program {
  static void Main() {
    var type = typeof(Person);
    var obj = Activator.CreateInstance(type);
    Console.WriteLine(((Person)obj).Name);
  }
}
This code creates a Person object dynamically and prints its Name property.
Execution Table
StepActionEvaluationResult
1Get type infotypeof(Person)Person type info stored in 'type'
2Create instanceActivator.CreateInstance(type)New Person object created, stored in 'obj'
3Access property((Person)obj).Name"Unknown"
4Print outputConsole.WriteLineOutputs: Unknown
5EndNo more codeProgram ends
💡 All steps completed, instance created and used successfully.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
typenullPerson type infoPerson type infoPerson type infoPerson type info
objnullnullPerson instancePerson instancePerson instance
NameN/AN/AN/A"Unknown""Unknown"
Key Moments - 2 Insights
Why do we need to cast 'obj' to Person before accessing 'Name'?
Because Activator.CreateInstance returns an object of type 'object', we must cast it to 'Person' to access Person-specific members like 'Name'. See execution_table step 3.
What happens if the type does not have a parameterless constructor?
Activator.CreateInstance will throw an exception because it requires a parameterless constructor to create the instance. This is implied in step 2 where creation succeeds.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'Name' after step 3?
Anull
B"Unknown"
C"Person"
DThrows error
💡 Hint
Check the 'Result' column in step 3 of execution_table.
At which step is the new Person object created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for object creation.
If we forget to cast 'obj' to Person, what happens when accessing 'Name'?
AIt prints "Unknown" anyway
BRuntime error
CCompilation error
DIt prints null
💡 Hint
Casting is needed to access Person members; see key_moments explanation.
Concept Snapshot
Creating instances dynamically in C#:
- Use typeof(TypeName) to get type info.
- Use Activator.CreateInstance(type) to create an object.
- Cast the object to the correct type to access members.
- Requires a parameterless constructor.
- Useful for flexible, runtime object creation.
Full Transcript
This example shows how to create an instance of a class dynamically in C#. First, we get the type information using typeof. Then, we call Activator.CreateInstance with that type to create a new object. Since the method returns a generic object, we cast it to the specific class to access its properties. Finally, we print the Name property which is set to "Unknown" by the constructor. This process allows creating objects when the type is not known until runtime.