With best practices in mind, what’s the advisable approach to adding optional parameters to a constructor signature? Should you only ever list the core parameters and depend on initialisers for the non optional properties? Seems to me to be the most sensible approach!
With best practices in mind, what’s the advisable approach to adding optional parameters to
Share
In particular with the ability to assign property values with object initializers, multiple overloads for constructors are far less useful than they were. They were mostly useful in the past for setting mutable properties for code compactness.
Now one can write
Almost as compact as
while being more expressive.
However, sometimes the nature of an object dictates that an overload would provide a meaningful contract to the object consumer. For example, providing an overload for a Rectangle class that accepts a length and a width is not unreasonable (I would not ding it on a code review). Personally I would still not provide an overload because object initializer syntax is more expressive, avoiding questions like “was that Rectangle(int length, int width) or Rectangle(int width, int length)?” while reading code (I know, intellisense helps while writing code).