Is there a way to create a property that can be changed within the class, but still accessed with objectName.propertyName
outside of it? I'm curious if I can access the property without a get method and still not be able to change it outside of the class.
Obviously, readonly
doesn't work, because it restricts changes within the class as well.
You can set a different access level on get
or set
. So this:
public class MyClass
{
public string MyStringProperty
{
get;
private set;
}
}
gives you a property that can only be read on the outside, but set by the class itself.
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