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

Creating instances dynamically in C Sharp (C#) - Quick Revision & Summary

Choose your learning style9 modes available
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?
AObject.NewInstance()
Bnew Type()
CActivator.CreateInstance(Type)
DType.Create()
What does typeof(MyClass) return in C#?
AThe Type object representing MyClass
BThe name of MyClass as a string
CAn instance of MyClass
DA new object of type object
What is required for a class to be instantiated using Activator.CreateInstance without parameters?
AA private constructor
BNo constructor at all
CA static method named Create
DA public parameterless constructor
Why would you create instances dynamically in a program?
ATo hardcode all objects at compile time
BTo allow flexibility and create objects based on runtime info
CTo make the program run slower
DTo avoid using classes
Which namespace do you need to use Activator.CreateInstance?
ASystem
BSystem.IO
CSystem.Threading
DSystem.Collections
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.