Recall & Review
beginner
What does the
new keyword do in C#?The <code>new</code> keyword creates a new instance (object) of a class in memory, allowing you to use that object in your program.Click to reveal answer
beginner
How do you create a new object of a class named <code>Car</code>?You write: <br>
Car myCar = new Car(); <br>This creates a new Car object and stores it in the variable myCar.Click to reveal answer
beginner
Why do you need to use
new to create objects?Using
new tells the computer to allocate memory for the object and run its constructor to set it up properly.Click to reveal answer
intermediate
What happens if you declare a variable of a class type without using <code>new</code>?The variable will be null (empty) and does not point to any object. Trying to use it will cause a NullReferenceException at runtime.
Click to reveal answer
beginner
Can you instantiate an object without parentheses after the class name? For example: <code>new Car;</code>No, in C# you must include parentheses even if the constructor has no parameters:
new Car(); is correct.Click to reveal answer
What does
new do in C#?✗ Incorrect
new creates a new object instance in memory.Which is the correct way to instantiate a class named
Dog?✗ Incorrect
You must use
new Dog(); with parentheses to create a new object.What happens if you declare
Car myCar; without new?✗ Incorrect
Without
new, the variable is null and does not reference an object.Why do you need parentheses after the class name when using
new?✗ Incorrect
Parentheses call the constructor to initialize the new object.
Which statement is true about object instantiation?
✗ Incorrect
new allocates memory and creates the object instance.Explain in your own words what happens when you use
new to create an object in C#.Think about what the computer does behind the scenes when you write <code>new ClassName()</code>.
You got /4 concepts.
Describe the difference between declaring a variable of a class type and instantiating an object with
new.Consider what the variable holds before and after using <code>new</code>.
You got /4 concepts.