2 ms·
> Can records implement interfaces? Yes. > Can records be variants in a sealed class hierarchy? Yes. > Can records have a private primary constructor? The
by pron 6y ago
> Can records implement interfaces?
Yes.
> Can records be variants in a sealed class hierarchy?
Yes.
> Can records have a private primary constructor?
The accessibility of the constructor is roughly at least that of the record class itself. If the record class is public, then the canonical constructor must be public. However, thanks to the positive answers to the two previous questions, you can have a readable-though-not-publicly-constructible record -- make it a private implementation of a sealed public interface.
> Can I customize the getters (to e.g., make defensive copies)?
Yes. Although in most cases, defensive copying in the constructor is sufficient.