I'm starting out as a C# enthusiast and it seems to me that properties should always be public. Private property wouldn't make sense. Would it?
Would this:
private string propertyOne {get; set;}
be equivalent to this:
public string propertyOne {private get; private set;}
Yes, private properties can make sense, particularly in cases where you have logic you want to implement in the getters/setters. You may only want these accessible within the class (hence they're private) but you still want to encapsulate the getter/setter logic in one place.
There is a difference between the two lines of code you printed. Someone reflecting over public properties won't see the first one but they will see the second, even if they can't invoke the getter/setter.
The idea of using a Property is to encapsulate rather than just present a raw variable to the 'outside world'. That way you can also have an extra logic in your accessors.
So No, a purely private property wouldn't be the usual use case.
It's not uncommon to see public properties with a private setter though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With