Recall & Review
beginner
What does it mean to create instances dynamically in C#?
Creating instances dynamically means making objects at runtime using code, not just by writing them directly in the program. This allows the program to decide which objects to create while it runs.
Click to reveal answer
beginner
Which C# method is commonly used to create an instance of a class dynamically using its type?The method <code>Activator.CreateInstance(Type type)</code> is used to create an instance of a class dynamically when you have the <code>Type</code> object representing that class.Click to reveal answer
beginner
How can you get the <code>Type</code> object of a class named <code>Car</code> in C#?You can get the <code>Type</code> object by using <code>typeof(Car)</code> if you know the class at compile time, or by <code>Type.GetType("Namespace.Car")</code> if you have the class name as a string.Click to reveal answer
intermediate
What is a practical use case for creating instances dynamically?
A practical use is when you want to load plugins or modules at runtime without knowing their types beforehand. Your program can create objects from these modules dynamically based on user input or configuration.
Click to reveal answer
intermediate
What must be true about a class to create its instance dynamically using <code>Activator.CreateInstance</code>?The class must have a public parameterless constructor, or you must provide the constructor parameters when calling <code>Activator.CreateInstance</code>.Click to reveal answer
Which C# method creates an object dynamically from a Type?
✗ Incorrect
Activator.CreateInstance(Type) is the correct method to create an instance dynamically from a Type object.
What does
typeof(MyClass) return in C#?✗ Incorrect
typeof(MyClass) returns the Type object that represents the class MyClass.What is required for a class to be instantiated using
Activator.CreateInstance without parameters?✗ Incorrect
A public parameterless constructor is needed to create an instance without passing parameters.
Why would you create instances dynamically in a program?
✗ Incorrect
Creating instances dynamically allows the program to decide which objects to create while running, adding flexibility.
Which namespace do you need to use
Activator.CreateInstance?✗ Incorrect
Activator.CreateInstance is part of the System namespace, but often used with System.Reflection for dynamic type handling.Explain how you can create an instance of a class dynamically in C# and why this might be useful.
Think about how your program can create objects when it doesn't know the exact class at compile time.
You got /4 concepts.
Describe the requirements a class must meet to be instantiated dynamically using Activator.CreateInstance without parameters.
Consider what kind of constructor the class needs for this method to work smoothly.
You got /3 concepts.