Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A
- {
- // Note: List is just example, I am interested in objects in general.
- private List<string> _items = new List<string>() { "hello" }
- public List<string> Items
- {
- get
- {
- // Option A (not read-only), can be modified from outside:
- return _items;
- // Option B (sort-of read-only):
- return new List<string>( _items );
- // Option C, must change return type to ReadOnlyCollection<string>
- return new ReadOnlyCollection<string>( _items );
- }
- }
- }
- List<string> someList = ( new A() ).Items;
- public class A
- {
- private int _n;
- public int N
- {
- get { return _n; }
- set { _n = value; }
- }
- }
- public interface IReadOnlyA
- {
- public int N { get; }
- }
- public class A : IReadOnlyA
- {
- private int _n;
- public int N
- {
- get { return _n; }
- set { _n = value; }
- }
- }
Add Comment
Please, Sign In to add comment