Readonly property

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.
 
B

Barry Kelly

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top