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

Object instantiation with new in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
ADeclares a variable
BDeletes an object
CCreates a new object instance
DCalls a method
Which is the correct way to instantiate a class named Dog?
ADog d = Dog();
BDog d = new Dog();
CDog d = new Dog;
DDog d = new;
What happens if you declare Car myCar; without new?
AmyCar is null and unusable
BmyCar is a new object
CmyCar is a number
DmyCar is a string
Why do you need parentheses after the class name when using new?
AParentheses are optional
BTo declare a variable
CTo assign a value
DTo call the constructor method
Which statement is true about object instantiation?
AThe <code>new</code> keyword allocates memory for the object
BYou can create an object without <code>new</code>
CObjects are created automatically without code
DVariables hold objects directly without references
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.