Bird
Raised Fist0

In C#, what is the primary effect of using new when you write var obj = new SampleClass();?

easy🧠 Conceptual Q1 of Q15
C Sharp (C#) - Classes and Objects
In C#, what is the primary effect of using new when you write var obj = new SampleClass();?
AIt converts a value type to a reference type.
BIt declares a variable without allocating memory.
CIt copies an existing object into a new variable.
DIt allocates memory and calls the constructor to initialize the object.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the new keyword

    The new keyword in C# is used to create an instance of a class.
  2. Step 2: Memory allocation and constructor call

    When new SampleClass() is executed, it allocates memory for the object and calls the constructor to initialize it.
  3. Final Answer:

    It allocates memory and calls the constructor to initialize the object. -> Option D
  4. Quick Check:

    Using new creates and initializes an object. [OK]
Quick Trick: new allocates memory and calls constructor [OK]
Common Mistakes:
MISTAKES
  • Thinking new only declares a variable
  • Assuming new copies existing objects
  • Confusing new with type conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes