Readonly property

  • Thread starter Thread starter vze1r2ht
  • Start date Start date
V

vze1r2ht

Is there a way in C# for a property to be Write-enabled from a certain
class and readonly from outside classes?

For an example, I have two objects User and UserManager.

I want certain properties that should be readonly be set by the
UserManager. Other classes should only be able to read that property.

Such properties include, User.Exists and User.DateCreated etc.
 
Is there a way in C# for a property to be Write-enabled from a certain
class and readonly from outside classes?

The usual solution here is to use internal visibility for things that
need to be writable by classes in the same assembly. For example:

---8<---
class Foo
{
private int _value;
public int Value
{
get { return _value; }
internal set { _value = value; }
}
}
--->8---

-- Barry
 
Back
Top