Questions tagged [readonly-collection]

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

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

105 questions
12
votes
3 answers

Is there anything magic about ReadOnlyCollection

Having this code... var b = new ReadOnlyCollection(new[] { 2, 4, 2, 2 }); b[2] = 3; I get a compile error at the second line. I would expect a runtime error since ReadOnlyCollection implements IList and the this[T] have a setter in the…
Esben Skov Pedersen
  • 4,437
  • 2
  • 32
  • 46
10
votes
1 answer

Which way of returning a readonly wrapper of List is preferable?

Say, we have a generic class with a private List. We can make it return a readonly wrapper of this list at least with two ways: public class Test { public List list = new List(); public IEnumerable Values1 { get …
horgh
  • 17,918
  • 22
  • 68
  • 123
9
votes
1 answer

protobuf-net: Failing to deserialize ReadOnlyCollection

I'm trying to serialize and deserialize a ReadOnlyCollection using protobuf-net. However an exception is thrown upon deserialization when protobuf-net attempts to cast a List into a ReadOnlyCollection. var roc = new…
Mark
  • 510
  • 1
  • 5
  • 18
9
votes
5 answers

Public List without Add

public class RegistrationManager { public List RegisteredObjects; public bool TryRegisterObject(object o) { // ... // Add or not to Registered // ... } } I want that RegisterObjects be accessible from outside of the…
Nicolas Voron
  • 2,916
  • 1
  • 21
  • 35
9
votes
1 answer

Why is covariance not allowed with ReadOnlyCollection?

The reason this code works is due to the fact that the Enumerator cannot modify the collection: var roList = new List() { "One", "Two", "Three" }; IEnumerable objEnum = roList; If we try to do the same thing with a List,…
B.K.
  • 9,982
  • 10
  • 73
  • 105
9
votes
3 answers

Why am I getting error: "cannot implicitly convert type System.Collections.Generic.List"

I have the following sealed class. I'm trying to return the list as a ReadOnlyCollection. Tried a couple of things but I'm not getting the hang of this. So how do I return or cast the list to the readonly collection? public sealed class…
Debbie
  • 125
  • 1
  • 1
  • 6
8
votes
1 answer

What is difference between ReadOnlyCollection and ReadOnlyCollectionBuilder in .Net?

Today I came across a dilemma what is the difference between ReadOnlyCollection and ReadOnlyCollectionBuilder in .Net? In ReadOnlyCollection object we cannot add and remove items. Where as in ReadOnlyCollectionBuilder object we can add…
Dharmesh Tailor
  • 320
  • 1
  • 11
8
votes
2 answers

How does ReadOnlyCollection hide Add and Remove methods

ReadOnlyCollection realises the ICollection interface which has methods like Add and Remove. I know how to hide methods from Intellisense using attributes, but how is it possible to cause an actual compilation error if I try to use these…
RichK
  • 11,318
  • 6
  • 35
  • 49
8
votes
2 answers

How to concatenate ReadOnlyCollection

The ReadOnlyCollection constructor requires that you give it IList. But if you have some ROC's that you want to concatenate and produce a new ROC, the Concat method returns IEnumerable. Which is not a valid argument to pass to the ROC…
Edward Ned Harvey
  • 6,525
  • 5
  • 36
  • 45
7
votes
4 answers

Why doesn't ReadOnlyCollection<> include methods like FindAll(), FindFirst(),

Following the suggestions of FxCop and my personal inclination I've been encouraging the team I'm coaching to use ReadOnlyCollections as much possible. If only so that recipients of the lists can't modify their content. In their theory this is bread…
Mark Levison
  • 788
  • 3
  • 9
  • 22
7
votes
1 answer

What read-only, order-preserving collection in C# should I use to support enumeration?

I have only two requirements for a data structure: Be read-only, Preserve order (I want to enumerate it in specific order, always). I know that IReadOnlyList does preserve order. I could use it, but I don't need indexing. That implies I should use…
ebvtrnog
  • 4,167
  • 4
  • 31
  • 59
7
votes
4 answers

"? extends ParentClass" makes Read only?

In the following code Java, I have created a list nums. I can assign the another list during the declaration. But new items cannot be added except the null. So, does it mean the nums is readonly? Why? Is it possible to add new items in that…
Pradip Kharbuja
  • 3,442
  • 6
  • 29
  • 50
6
votes
0 answers

Why doesn't IDictionary implement IReadOnlyDictionary?

I noticed that Dictionary Class implements IDictionary as well as IReadOnlyDictionary. However IDictionary Interface does not implement IReadOnlyDictionary. This means that if my class has an…
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
6
votes
3 answers

ReadonlyCollection, are the objects immutable?

I'm trying using ReadOnlyCollection to make object immutable, I want the property of object are immutable. public ReadOnlyCollection MyReadOnlyList { get { return new ReadOnlyCollection(_myDataList); …
lunatic84
  • 300
  • 4
  • 12
5
votes
2 answers

MVVM - Should I expose ReadOnlyObservableCollection, ReadOnlyCollection, ICollection, etc?

Basically, I was always in the understanding that you should return the expose base types whenever you can and worry about implementation details internally, which makes sense... But, I'm not sure what to do here. Basically, right now I…
michael
  • 14,844
  • 28
  • 89
  • 177