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

Creating instances dynamically in C Sharp (C#) - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an instance of the class Person.

C Sharp (C#)
Person person = new [1]();
Drag options to blanks, or click blank then click option'
Aperson
Bnew
CPerson
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name instead of the class name.
Omitting the parentheses after the class name.
2fill in blank
medium

Complete the code to create an instance dynamically using Activator.CreateInstance.

C Sharp (C#)
object obj = Activator.CreateInstance(typeof([1]));
Drag options to blanks, or click blank then click option'
APerson
BActivator
Cobject
DCreateInstance
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable name instead of the class name.
Forgetting to use typeof().
3fill in blank
hard

Fix the error in the code to create an instance dynamically with a parameterless constructor.

C Sharp (C#)
var instance = (Person)Activator.CreateInstance([1]);
Drag options to blanks, or click blank then click option'
Atypeof(Person)
BActivator
Cnew Person()
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class name as a string or identifier without typeof().
Trying to pass an instance instead of a type.
4fill in blank
hard

Fill both blanks to create an instance dynamically and cast it to the correct type.

C Sharp (C#)
var instance = ([1])Activator.CreateInstance([2]);
Drag options to blanks, or click blank then click option'
APerson
Btypeof(Person)
Cobject
DActivator
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to object instead of the class type.
Passing the class name without typeof().
5fill in blank
hard

Fill all three blanks to create an instance dynamically with parameters using Activator.CreateInstance.

C Sharp (C#)
var instance = ([1])Activator.CreateInstance([2], new object[] { [3] });
Drag options to blanks, or click blank then click option'
APerson
Btypeof(Person)
C"John"
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Not casting the result to the class type.
Passing constructor arguments incorrectly.
Forgetting to use typeof().