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

后端 未结 7 2300
不思量自难忘°
不思量自难忘° 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条回答
  •  不要未来只要你来
    2021-02-02 07:53

    i agree with ReadOnlyCollection

    See my simple code:

     private List _devices;
    public readonly System.Collections.ObjectModel.ReadOnlyCollection 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....

提交回复
热议问题