Friday, January 15, 2021

C# Nominal versus Positional Object Creation

C# 9.0 introduces positional record types. Before discussing this language feature, a review is needed of the different styles of object creation in C#:

  • Positional: the properties of a class that require initialization are passed as parameters to a constructor. The constructor initializes the properties by assigning parameters to properties.
  • Nominal: the constructor for a class takes no parameters or fewer parameters than the number of properties that require initialization. When the object is constructed the properties are assigned using an object initializer.

To clarify the terms positional, nominal and object initializer consider the following class used to represent a technical certification: 

An example of positional object creation is as follows:

Line 30 shows positional object creation and line 33 shows positional object creation C# 9.0 fit and finish.

An example of nominal object creation is as follows where object initializes used to assign the Name and Description properties:

Line 30 shows nominal object creation and line 33 shows nominal object creation C# 9.0 fit and finish.


No comments :

Post a Comment