Questions tagged [readonly-collection]

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

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

105 questions
3
votes
2 answers

Best way to expose ReadOnlyCollection of ReadOnlyCollection while still being able to modify it inside class

I don't know if this is possible, but basically, I want to expose a property of type ReadOnlyCollection>, while still being able to modify the underlying collection inside the class exposing the property. Further, I want…
SaldaVonSchwartz
  • 3,769
  • 2
  • 41
  • 78
3
votes
1 answer

Why is the SortedList(TKey,TValue).Keys property an IList(TKey) rather than a ReadOnlyCollection(TKey)?

The IList interface includes access by index in addition to operations not supported by the SortedList.Keys property such as Add, Remove, and Insert. A ReadOnlyCollection, such as the return value of List.AsReadOnly,…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
3
votes
4 answers

How to prevent a method caller from modifying a returned collection?

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 GetFoos() { return this.foos; } At the moment the private…
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
2
votes
2 answers

How do you properly deserialize a class that has an IReadOnlyCollection using System.Text.Json?

I have the following class: public sealed class SomeClass { [JsonConstructor()] public SomeClass(IEnumerable myItems) { InternalMyItems = new Collection(myItems.ToArray()); MyItems = new…
myermian
  • 31,823
  • 24
  • 123
  • 215
2
votes
1 answer

Is it possible to create a non-modifiable hashtable in PowerShell?

I am familiar with creating read-only variables and constants in PowerShell by using the Set-Variable command with something like this: Set-Variable -Option ReadOnly, AllScope -Name STANDARD_TOKEN_PARAMS -Force -Value @{ Username =…
Efie
  • 1,430
  • 2
  • 14
  • 34
2
votes
1 answer

ReadOnlyCollection with read-only items

I have a class which contains a Collection (Collection myItems). I want to be able to modify this Collection privately and return it as read-only through a public property (in O(1) if possible). I was thinking about using a…
dCake
  • 47
  • 6
2
votes
2 answers

JS equivalent to Java's Collections.unmodifiableCollection

I often use this strategy to my java code in order to make a Collection read only for the outside world, but avoid big/often clonings: public abstract class MyClass { List myIds; public Collection getIds() { return…
Alkis Mavridis
  • 1,090
  • 12
  • 28
2
votes
1 answer

Using ReadOnlyCollection vs List vs array for return types

I posted a similar question a while ago concerning parameters, and it referred to an article more pertinent to this question, which advocates using IReadOnlyCollection over IEnumerable. Lately, I’ve been considering the merits and demerits of…
Neo
  • 4,145
  • 6
  • 53
  • 76
2
votes
5 answers

Making collection items read only

I have a business entities as below, class Class1 { List classes = new List(); public IEnumerable Classes { get { return classes.AsEnumrable(); } public void AddClass(Class2 cls) { classes.Add(cls); …
Sisyphus
  • 900
  • 12
  • 32
2
votes
2 answers

ReadOnlyCollection property underlying Collection is modified while iterating

I have the following property: private static Collection _loadedAssemblies = new Collection(); internal static ReadOnlyCollection LoadedAssemblies { get { return new…
Sybren
  • 1,071
  • 3
  • 17
  • 51
2
votes
1 answer

How does List copy constructor function with ReadOnly lists?

The MSDN article doesn't really explain this. List FirstList = new List(); // Add items to FirstList. List SecondList = new List(FirstList.AsReadOnly()); // Is SecondList a read-only collection?
Giffyguy
  • 20,378
  • 34
  • 97
  • 168
2
votes
1 answer

How to flatten out the concatenation of IEnumerables of IEnumerables

I know there's a LINQ solution somewhere, I just can't see it. The situation I have is this: I have a base class (lets call it B, for now) that accepts a vararg and uses it to initialize a readonly field, which happens to be a IReadonlyCollection…
FinnTheHuman
  • 1,115
  • 13
  • 29
2
votes
2 answers

Selenium: FindsBy with collection

I am a beginner in testing and have a question. How can I correctly use ReadOnlyCollectionif I use attribute FindsBy . My collection is always null after started test. Heres is my code in C#: [FindsBy(How = How.Name,…
Deyeth
  • 59
  • 2
  • 11
2
votes
2 answers

C#, SynchronizedReadOnlyCollection and its constructors

.net class SynchronizedReadOnlyCollection has 4 constructors. public SynchronizedReadOnlyCollection(); public SynchronizedReadOnlyCollection(object syncRoot); public SynchronizedReadOnlyCollection(object syncRoot, IEnumerable list); …
TTT
  • 2,365
  • 17
  • 16
2
votes
2 answers

Writing ReadOnlyCollection to stream

I work with a binary format that contains several magic byte sequences. I want to keep them in a static class as immutable static members. public static class HuffmanConsts { // output format: Header, serialized tree (prefix), DataDelimiter,…
Palec
  • 12,743
  • 8
  • 69
  • 138