Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties that are read-only, but only outside of the class

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.

like image 922
Wilson Avatar asked Oct 17 '25 19:10

Wilson


1 Answers

You can set a different access level on getor 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.

like image 73
Jan Dörrenhaus Avatar answered Oct 20 '25 07:10

Jan Dörrenhaus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!