Pregunta

I've read many opinions that public fields are evil, public properties are by designed to be public and we can do super great things with setters and getters etc.

My understanding is, although minimal, readonly fields are a performance hit (and, related to performance or not, I know both FXCop and Resharper prompt me to use readonly where ever possible).

I know we can't have a readonly property in .NET but, are readonly public fields still evil?

¿Fue útil?

Solución

One of the reasons why public fields are evil is that if 100 other assemblies are compiled against that field, and later you decide to replace it with a public property (to include validation, etc), all those assemblies will have to be recompiled as well.

So, it really doesn't matter whether the field is readonly or not - public fields are still evil.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top