readonly-collection

NHibernate: use of IEnumerable as collection type results in error

时光怂恿深爱的人放手 提交于 2019-12-11 09:55:37
问题 I have a class which uses an ISet as a collection type as below: public class Client { private ISet<Contact> _contacts = new HashedSet<Contact>(); public virtual ISet<Contact> Contacts { get { return _contacts; } } } I don't want the collection itself to be able to be modified externally. However, if I change the property's type to IEnumerable as below: public class Client { private ISet<Contact> _contacts = new HashedSet<Contact>(); public virtual IEnumerable<Contact> Contacts { get { return

Make a list readonly in c#

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 04:18:01
问题 I have this example code. What I want to do is to make it so that the "Nums" value can only be written to using the "AddNum" method. namespace ConsoleApplication1 { public class Person { string myName = "N/A"; int myAge = 0; List<int> _nums = new List<int>(); public List<int> Nums { get { return _nums; } } public void AddNum(int NumToAdd) { _nums.Add(NumToAdd); } public string Name { get; set; } public int Age { get; set; } } } Somehow, I've tried a bunch of things regarding AsReadOnly() and

How to prevent a method caller from modifying a returned collection? [duplicate]

旧巷老猫 提交于 2019-12-07 15:57:29
问题 This question already has answers here : ReadOnlyCollection or IEnumerable for exposing member collections? (5 answers) Closed 6 years ago . I have methods returning private collections to the caller and I want to prevent the caller from modifying the returned collections. private readonly Foo[] foos; public IEnumerable<Foo> GetFoos() { return this.foos; } At the moment the private collection is a fixed array, but in the future the collection might become a list if the need for adding new

ReadOnlyCollection vs Liskov - How to correctly model immutable representations of a mutable collection

孤人 提交于 2019-12-06 18:22:01
问题 Liskov-substitution principle requires that subtypes must satisfy the contracts of super-types. In my understanding, this would entail that ReadOnlyCollection<T> violates Liskov. ICollection<T> 's contract exposes Add and Remove operations, but the read only subtype does not satisfy this contract. For example, IList<object> collection = new List<object>(); collection = new System.Collections.ObjectModel.ReadOnlyCollection<object>(collection); collection.Add(new object()); -- not supported

Make a list readonly in c#

心已入冬 提交于 2019-12-06 15:32:10
I have this example code. What I want to do is to make it so that the "Nums" value can only be written to using the "AddNum" method. namespace ConsoleApplication1 { public class Person { string myName = "N/A"; int myAge = 0; List<int> _nums = new List<int>(); public List<int> Nums { get { return _nums; } } public void AddNum(int NumToAdd) { _nums.Add(NumToAdd); } public string Name { get; set; } public int Age { get; set; } } } Somehow, I've tried a bunch of things regarding AsReadOnly() and the readonly keyword, but I can't seem to get it to do what I want it to do. Here is the sample of the

ReadOnlyCollection vs Liskov - How to correctly model immutable representations of a mutable collection

試著忘記壹切 提交于 2019-12-04 22:24:53
Liskov-substitution principle requires that subtypes must satisfy the contracts of super-types. In my understanding, this would entail that ReadOnlyCollection<T> violates Liskov. ICollection<T> 's contract exposes Add and Remove operations, but the read only subtype does not satisfy this contract. For example, IList<object> collection = new List<object>(); collection = new System.Collections.ObjectModel.ReadOnlyCollection<object>(collection); collection.Add(new object()); -- not supported exception There is clearly a need for immutable collections. Is there something broken about .NET's way of

How to properly use IReadOnlyDictionary?

时光怂恿深爱的人放手 提交于 2019-12-04 03:49:19
From msdn : Represents a generic read-only collection of key/value pairs. However consider following: class Test { public IReadOnlyDictionary<string, string> Dictionary { get; } = new Dictionary<string, string> { { "1", "111" }, { "2", "222" }, { "3", "333" }, }; public IReadOnlyList<string> List { get; } = (new List<string> { "1", "2", "3" }).AsReadOnly(); } class Program { static void Main(string[] args) { var test = new Test(); var dictionary = (Dictionary<string, string>)test.Dictionary; // possible dictionary.Add("4", "444"); // possible dictionary.Remove("3"); // possible var list =

Add to a readonly collection in a constructor?

北慕城南 提交于 2019-12-02 10:17:40
问题 Is there a c# language construct that will allow me to add items to a readonly collection property in a constructor? I want to do something like this: public class Node{ public IList<Node> Children {get; protected set;} public Node(){ Children = new ObservableList<Node>(); } } ... in my code somewhere... var node = new Node {Children.Add(new Node())}; (not a great example, but I hope this gets the idea across)... UPDATE OK sorry I need to be clearer. I didn't write this Node class, and I

Add to a readonly collection in a constructor?

送分小仙女□ 提交于 2019-12-02 05:33:07
Is there a c# language construct that will allow me to add items to a readonly collection property in a constructor? I want to do something like this: public class Node{ public IList<Node> Children {get; protected set;} public Node(){ Children = new ObservableList<Node>(); } } ... in my code somewhere... var node = new Node {Children.Add(new Node())}; (not a great example, but I hope this gets the idea across)... UPDATE OK sorry I need to be clearer. I didn't write this Node class, and I cannot change it. I am asking if there is a c# language concept that will allow me to add to the readonly

Using IReadOnlyCollection<T> instead of IEnumerable<T> for parameters to avoid possible multiple enumeration

ε祈祈猫儿з 提交于 2019-12-01 03:18:56
My question is related to this one concerning the use of IEnumerable<T> vs IReadOnlyCollection<T> . I too have always used IEnumerable<T> to expose collections as both return types and parameters because it benefits from being both immutable and lazily executed. However, I am becoming increasingly concerned about the proliferation of places in my code where I must enumerate a parameter to avoid the possible multiple enumeration warning that ReSharper gives. I understand why ReSharper suggests this, and I agree with the code it suggests (below) in order to ensure encapsulation (i.e., no