I got a question about Constructors.I think constructors are all just our convenience instead of setter methods,right? So for an object , the properties you think important(such as required fields in a webform)are passed as parameters into constructor.
Is there any criteria that these many number of parameters should be passed into constructor?
Please elaborate on these points and as well as any points about constructors.
Edit:Sorry about the way i asked question.Yes,we create an object with constructor and we assign values with setters but my question is about the comparison between default constructor with setters and constructor with explicit parametrs.
No, it’s not just a convenience instead of setters. In particular, a constructor will only be called once. Using constructor parameters, you can create immutable types which are given their values at construction time – that wouldn’t be possible with setters.
It’s generally not a great idea to have huge numbers of parameters, whether to a constructor or a normal method. If you find you have a lot of parameters, you may want to create a type representing all the related ones – that type may have a bunch of getters/setters. See
ProcessStartInfofor an example of this.