Simple object initializers

A simple object initializer looks like the following:

var sampleClaim = new Claim() {:ClaimId = "TestID"} 

Object initializers comprise one or more property initializer expressions, separated by commas, and enclosed by braces. A property initializer expression is the following, in order: a colon (:), a property name, an equal sign (=), and a value or any expression that results in a value.

:propertyName = value

For example, suppose you have the following code, which sets properties on a new file container by using assignment statements that follow the new statement:

var myFileContainer = new my.company.FileContainer()  // Create a new file container.

myFileContainer.DestFile = jarFile                    // Set the properties on the new file container.
myFileContainer.BaseDir = dir
myFileContainer.Update = true
myFileContainer.WhenManifestOnly = ScriptEnvironment.WHEN_EMPTY_SKIP

The following sample code is functionally equivalent to the preceding code but uses an object initializer to make the assignments within the bounds of in the new statement:

var myFileContainer = new my.company.FileContainer() {   // Create a new file container,
  :DestFile = jarFile, :BaseDir = dir, :Update = true,   // and set its properties at creation time.
  :WhenManifestOnly = ScriptEnvironment.WHEN_EMPTY_SKIP
}