Does Apex have an equivalent to the C# object initializer?

The correct answer is it depends.

Currently salesforce only accepts that for sObjects. Other Objects (1) cannont be initialized that way.

You can however, create custom constructors like:

public class Student{

        private String firstName;
        private String lastName;

        public Student(String firstName, String lastName){
            this.firstName = firstName;
            this.lastName = lastName;
        }
}

(1) as per Derek's comment: an object defined in an Apex class using the 'class' keyword, can be a top level class or a inner class


This is the syntax

Student student = new Student(FirstName = "Jane",LastName = "Doe");

You can create a sObject and initialise its properties using the following examples.

// Create a Student instance and set some property values
Student student = new Student(
    FirstName = "Jane",
    LastName = "Doe",
);

Student student2 = new Student();
student2.FirstName = "Jim";
student2.LastName = "Doe";

I would suggest taking a look into this Trailhead module that will guide you through using sObjects.

Tags:

C#

Apex