C# return a variable as read only from get; set;

后端 未结 7 2162
不思量自难忘°
不思量自难忘° 2021-02-02 07:30

I swear I have seen an example of this but have been googling for a bit and can not find it.

I have a class that has a reference to an object and need to have a GET; met

相关标签:
7条回答
  • i agree with ReadOnlyCollection

    See my simple code:

     private List<Device> _devices;
    public readonly System.Collections.ObjectModel.ReadOnlyCollection<Device> Devices 
    {
     get
     { 
    return (_devices.AsReadOnly());
     } 
    

    }

    ReadOnlyCollection dosen't has Add method so user cant add properties to it.BUT ther is no warranty that if user can modify objects by calling their methods....

    0 讨论(0)
  • 2021-02-02 07:55

    This isn't possible. Get and set accessors to reference types get and set the reference to the object. You can prevent changes to the reference by using a private (or internal) setter, but you cannot prevent changes to the object itself if it's exposed by a getter.

    0 讨论(0)
  • 2021-02-02 07:59

    Your question reads like you're looking for:

    public PropertyName { get; private set; }
    

    But then, given the answers so far I'm not sure I'm interpreting your question correctly. Besides, who am I to question Jon Skeet? :)

    0 讨论(0)
  • 2021-02-02 08:06

    If the object isn't too complicated/extensive then write an wrapper around it.

    for example:

    class A {
        public string strField = 'string';
        public int intField = 10;
    }
    
    class AWrapper {
        private A _aObj;
    
        public AWrapper(A aobj) {
          _aObj = A;
        }
    
        public string strField {
             get {
                return _aObj.strField;
             }
        }
    
        public int intField {
             get {
                return _aObj.intField;
             }
        }
    }
    

    So now all you do is give your client code an instance of the AWrapper class so that they may only use what you allow them to see.

    this may get a bit complicated and may not scale well if your base class is not set in stone, but for most simple situation it may just do the trick. I think this is called a facade pattern(but don't quote me on that =) )

    0 讨论(0)
  • 2021-02-02 08:08

    No, there's no way of doing this. For instance, if you return a List<string> (and it's not immutable) then callers will be able to add entries.

    The normal way round this is to return an immutable wrapper, e.g. ReadOnlyCollection<T>.

    For other mutable types, you may need to clone the value before returning it.

    Note that just returning an immutable interface view (e.g. returning IEnumerable<T> instead of List<T>) won't stop a caller from casting back to the mutable type and mutating.

    EDIT: Note that apart from anything else, this kind of concern is one of the reasons why immutable types make it easier to reason about code :)

    0 讨论(0)
  • 2021-02-02 08:10

    Return a reference to a stripped-down interface:

     interface IFoo
       string Bar { get; }
    
     class ClassWithGet
       public IFoo GetFoo(...);
    
    0 讨论(0)
提交回复
热议问题