Microsoft Certified Solutions Developer (MCSD) Certification 2025 – 400 Free Practice Questions to Pass the Exam

Question: 1 / 400

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

Person p = new Person();

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.

Get further explanation with Examzify DeepDiveBeta

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

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

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy