Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name instead of the class name.
Omitting the parentheses after the class name.
✗ Incorrect
To create an instance of a class, you use the class name after the
new keyword.2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable name instead of the class name.
Forgetting to use
typeof().✗ Incorrect
You pass the type of the class you want to create an instance of to
Activator.CreateInstance using typeof(ClassName).3fill in blank
hardFix 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'
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.
✗ Incorrect
You must pass a
Type object to CreateInstance, so use typeof(Person).4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to
object instead of the class type.Passing the class name without
typeof().✗ Incorrect
You cast the created object to the class type and pass the type using
typeof().5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not casting the result to the class type.
Passing constructor arguments incorrectly.
Forgetting to use
typeof().✗ Incorrect
You cast to the class type, pass the type with
typeof(), and provide constructor arguments as an object array.