Questions tagged [readonly-collection]

Represents a strongly-typed, read-only collection of elements.

Represents a strongly-typed, read-only collection of elements.

105 questions
139
votes
5 answers

ReadOnlyCollection or IEnumerable for exposing member collections?

Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection? class Bar { private ICollection foos; // Which one is to be preferred? …
Erik Öjebo
  • 10,821
  • 4
  • 54
  • 75
108
votes
7 answers

Read-only list or unmodifiable list in .NET 4.0

From what I can tell, .NET 4.0 still lacks read-only lists. Why does the framework still lack this functionality? Isn't this one of the commonest pieces of functionality for domain-driven design? One of the few advantages Java has over C# is this in…
Chris S
  • 64,770
  • 52
  • 221
  • 239
40
votes
6 answers

How to make dictionary read-only?

I have a class like: class A: def __init__(self): self.data = {} and at some moment I want to prohibit self.data fields modification. I've read in PEP-416 rejection notice that there are a lot of ways to do it. So I'd like to find what…
sshilovsky
  • 958
  • 2
  • 9
  • 22
32
votes
5 answers

Why IReadOnlyCollection has ElementAt but not IndexOf

I am working with a IReadOnlyCollection of objects. Now I'm a bit surprised, because I can use linq extension method ElementAt(). But I don't have access to IndexOf(). This to me looks a bit illogical: I can get the element at a given position, but…
Lorenzo Santoro
  • 464
  • 1
  • 6
  • 16
30
votes
2 answers

How can I access a element of a IReadOnlyCollection through it index?

I am working with selenium and I am using the function FindElements so I am getting a element that implements IReadOnlyCollection interface. I want to iterate through the list but it seems that IReadOnlyCollection doesnt have any method like Get(int…
Yoiku
  • 882
  • 2
  • 13
  • 25
30
votes
5 answers

How to create an empty IReadOnlyCollection

I'm creating an extension method for MultiValueDictionary to encapsulate frequent ContainsKey checks and I was wondering what was the best way to create an empty IReadOnlyCollection?. What I've used so far is new List(0).AsReadOnly() but…
i3arnon
  • 113,022
  • 33
  • 324
  • 344
26
votes
6 answers

What is a read only collection?

I ran a security code analyst i found myself having a CA2105 warning. I looked at the grade tampering example. I didn't realize you can assign int[] to a readonly int. I thought readonly was like the C++ const and makes it illegal. The How to Fix…
user34537
23
votes
6 answers

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

Liskov-substitution principle requires that subtypes must satisfy the contracts of super-types. In my understanding, this would entail that ReadOnlyCollection violates Liskov. ICollection's contract exposes Add and Remove operations, but the…
20
votes
3 answers

Converting Dictionary> to ReadOnlyDictionary>

I have a dictionary as follows: public enum Role { Role1, Role2, Role3, } public enum Action { Action1, Action2, Action3, } var dictionary = new Dictionary>(); dictionary.Add(RoleType.Role1, new Action [] { Action.Action1,…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
20
votes
1 answer

How to properly use IReadOnlyDictionary?

From msdn: Represents a generic read-only collection of key/value pairs. However consider following: class Test { public IReadOnlyDictionary Dictionary { get; } = new Dictionary { { "1", "111" }, …
Sinatr
  • 20,892
  • 15
  • 90
  • 319
19
votes
2 answers

Entity Framework read only collections

Consider a domain where a Customer, Company, Employee, etc, etc, have a ContactInfo property which in turn contains a set of Address(es), Phone(es), Email(s), etc, etc... Here is my abbreviated ContactInfo: public class ContactInfo : Entity { …
zam6ak
  • 7,229
  • 11
  • 46
  • 84
16
votes
1 answer

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

My question is related to this one concerning the use of IEnumerable vs IReadOnlyCollection. I too have always used IEnumerable to expose collections as both return types and parameters because it benefits from being both immutable and…
Neo
  • 4,145
  • 6
  • 53
  • 76
15
votes
5 answers

C# HashSet read-only workaround

Here is this sample code: static class Store { private static List strList = new List(); private static HashSet strHashSet = new HashSet(); public static List NormalList { get { return…
VBTiger
  • 153
  • 1
  • 1
  • 7
14
votes
5 answers

Return ReadOnlyCollection from IList<>

OK, so List<> contains the AsReadOnly() which gives you the ReadOnlyCollection. What I need is to have a field of IList type, and a property which would return a ReadOnlyCollection for this list. Example class: class Test { private IList
Denis Biondic
  • 7,943
  • 5
  • 48
  • 79
12
votes
1 answer

Immutability/Read-only semantics (particular C# IReadOnlyCollection)

I am doubting my understanding of the System.Collection.Generic.IReadOnlyCollection semantics and doubting how to design using concepts like read-only and immutable. Let me describe the two natures I'm doubting between by using the documentation…
JBSnorro
  • 6,048
  • 3
  • 41
  • 62
1
2 3 4 5 6 7