Interfaces & Immutability

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Miguel Miguel
    New Member
    • Apr 2011
    • 2

    Interfaces & Immutability

    Good morning, afternoon or night,

    I have been wondering if there is any way one can enforce immutability only through the implementation of an interface. For example, if you design an interface, say IReadOnlyObject , containing only methods that can't change an object's state, you can of course do

    public sealed class ReadOnlyObject : IReadOnlyObject
    {
    // Place code here.
    }

    However, it is possible that you or anyone using the IReadOnlyObject defines in the actual implementation methods that can actually change an object's state. Isn't there any way to ensure that any object implementing IReadOnlyObject is, in fact, immutable?

    Thank you very much.
Working...