+ 1
[class & object] - Property procedure
Hi, I'd like to know if it's necessary to create a constructor of my class if i create property procedures for each variables of my class ? Thank you
4 Antworten
+ 1
The answer is "yes" and "no".
Yes: Each class must have at least one constructor.
No: You don't have to create it. If the compiler comes across a class without a constructor, it will create a default constructor during build time.
0
okey thank you.
0
but in a programme, it depend of the context, i can create a constructor? is it good todo that?
0
"i can create a constructor?"
Yes, you can definitely create your own construction. A couple of reasons I can think of why:
1. If you don't want the default parameter-less constructor to be used. For instance, you have a Person class and you want to guarantee the Name property is always provided. You would create a constructor with a "name" parameter.
2. Continuing example #1: Some time passes and you decide that you do, in fact, want to have the default parameter-less constructor available. In this case, you would be required to create this new constructor manually. Keep in mind, the compiler will only automatically create the default constructor if no constructors are declared explicitly by you.
"is it good todo that?"
My opinion? There is no need to manually create the default constructor for a class. A proficient developer will be aware that the compiler will create one, if needed.