Search code examples
ooppropertiesaccess-modifiers

Access modifiers on property getters


For specific context, I develop in C#, but I suppose this could be applied to other languages/frameworks.

I understand that creating a property with a private getter and a public setter is perfectly legal:

public int MyInt { private get; set; }

I'm having trouble figuring out why one would want to allow a client to set a property without being able get its current value. Under what circumstances might this type of behavior desired?


Solution

  • It's not the 'client' which changes the value, it's other parts of the program.

    It's possible that you want other objects have the ability to change the state of an object, via that property.
    The other parts of the object don't care for the state of that object however, it's kept for internal logic only.

    It all depends on the case and the structure of your application.