4 ms·
Constructors can definitely be misused. But they can be handy too. What if the initial values of the members are just constants. Perhaps it takes a bit of co
by ape4 7y ago
Constructors can definitely be misused. But they can be handy too. What if the initial values of the members are just constants. Perhaps it takes a bit of computing (not too much) to set them up.
- exDM69 7y ago> What if the initial values of the members are just constants In idiomatic rust impl Thing { fn new() -> Self { Thing { ival: 13, fval: 42.0 } } Another option is to use Default::default if appropriate.
- davesmith1983 7y agoC# has `readonly` variables. The values of these cannot be changed after the constructor has finished running.
- mattnewport 7y agoI don't find this that useful in C# since the majority of types in C# tend to be reference types and you can still change the values unless they are immutable types which though considered good practice aren't that common in most C# code in my experience.
- deleted 7y ago[deleted]