Microsoft Certified Solutions Developer (MCSD) Certification Practice Test

Disable ads (and more) with a membership for a one time $2.99 payment

Prepare for the Microsoft Certified Solutions Developer (MCSD) Exam with comprehensive quizzes, flashcards, and multiple-choice questions. Each query includes hints and clear explanations to help you succeed. Get exam-ready today!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


How do you simultaneously initialize an object and its properties in C#?

  1. Person p = new Person();

  2. Person p = new Person { FirstName = "John", LastName = "Doe" };

  3. Person p = new Person("John", "Doe");

  4. Person p = new Person { FirstName = "Doe", LastName = "John" };

The correct answer is: Person p = new Person { FirstName = "John", LastName = "Doe" };

The selected choice demonstrates an approach known as object initializer syntax in C#. This syntax allows developers to create an instance of an object and set its public properties in a concise manner. In this case, the statement `Person p = new Person { FirstName = "John", LastName = "Doe" };` initializes a new instance of the `Person` class while simultaneously assigning values to its `FirstName` and `LastName` properties. The use of curly braces `{}` indicates that you are providing initial values for the properties of the object being created. This can lead to cleaner code and improved readability, as it reduces the need for multiple statements to set properties after instantiation. The other options do not combine initialization and property assignment in this way. For example, creating an object without setting its properties will result in an instance with default values. Alternatively, using constructors (as seen in the third option) is another method of initialization, but it requires that the class has a suitable constructor defined to accept parameters, which may not be applicable to every situation or class design.