Questions tagged [readonly-collection]

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

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

105 questions
5
votes
3 answers

FxCop: CA1033 - Microsoft's implementation of a ReadOnlyCollection violates this?

If you look at the code for a read-only collection it does not have an "Add" method, but instead defines the ICollection.Add(T Value) method (explicit interface implementation). When I did something similar with my ReadOnlyDictionary class, FxCop…
myermian
  • 31,823
  • 24
  • 123
  • 215
5
votes
4 answers

Readonly collection properties that NHibernate can work with

My domain classes have collections that look like this: private List _foos = new List(); public virtual ReadOnlyCollection Foos { get { return _foos.AsReadOnly(); } } This gives me readonly collections that can be modified from…
David
  • 15,750
  • 22
  • 90
  • 150
5
votes
3 answers

How to pass a byte array readonly?

Think of the following code: static int Main() { byte[] data = File.ReadAllBytes("anyfile"); SomeMethod(data); ... } static void SomeMethod(byte[] data) { data[0] = anybytevalue; // this line should not be possible!!! byte b…
raisyn
  • 4,514
  • 9
  • 36
  • 55
5
votes
2 answers

Setting a read-only object from a derived class

I am writing a library that other developers in our company will use. A state machine base class has a ReadOnlyCollection of allowed states, etc. Developers need to inherit from this class and set the allowed states. I want to limit them to…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
4
votes
1 answer

How to prevent changes to an IReadOnlyList?

I need to use IReadOnlyList as a return parameter since it matches best my needs, but as you can see in the examples below, you can still modify the list it wraps if it is not truly read-only. using System.Collections.Generic; using…
aybe
  • 15,516
  • 9
  • 57
  • 105
4
votes
2 answers

Why can't I make an Array readonly this way in TypeScript?

The documentation shows (#ReadOnlyArray) how to do it using an interface, but as I am exploring the language I wondered why does this not work as well ? type TraverseTuple> = { readonly [P in keyof T]: T[P]; } const…
millsp
  • 1,259
  • 1
  • 10
  • 23
4
votes
2 answers

Pattern for forcing adding to collection through method in C#

I have a class with a collection member. I would like to prevent external code from modifying this collection directly, instead using methods (which can perform the appropriate validation etc). This is harder than I thought. Here is the solution I…
David
  • 15,750
  • 22
  • 90
  • 150
4
votes
1 answer

List and ReadOnly property

List (and List) instances can be readonly, seeing ReadOnly property; methods throws exceptions in the case the collection have the property ReadOnly property. How can I create readonly List instances? What are the main uses?
Luca
  • 11,646
  • 11
  • 70
  • 125
4
votes
2 answers

Make a list readonly in c#

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 =…
stumped221
  • 89
  • 4
  • 18
3
votes
2 answers

C# IReadOnlyList protection instantlly subverted by downcast, or am I missing something?

Given the below: Is it truly the case that if an IReadOnly(of T) was conceived from an extant MutableEquivalent(of T), that simply casting to the mutable version allows full access, and changes to an object others may rely on to have static…
schulmaster
  • 413
  • 5
  • 16
3
votes
1 answer

implicit/explicit cast confusion on IReadOnlyCollection

Why are different collections (that all implement the IReadOnlyCollection interface) handled differently by the compiler when trying to turn them into IReadOnlyCollection? IReadOnlyCollection a = new List(); // everything…
Kjara
  • 2,504
  • 15
  • 42
3
votes
1 answer

Why is ReadOnlyCollection> bad according to FxCop and what is the alternative when producing an immutable 2 dimensional object?

I am modifying all my code to conform to FxCop and this has meant ditching a lot of arrays, lists in favour of ReadOnlyCollection and I agree with this advice. However, when producing a ReadOnlyCollection> to replace a…
Pedruski
  • 41
  • 4
3
votes
3 answers

Read only list of lists c#

In general terms, a program I'm making involves storing a small number of entries (probably less than 30 at any given time) which can be categorized. I want to allow these entries to be seen but not altered from outside the class using them. I made…
3
votes
3 answers

Readonly List>

If we have field List>, how to expose it as a readonly property? To example: public class Test { private List> _list; } I can expose it like this public ReadOnlyCollection>…
Sinatr
  • 20,892
  • 15
  • 90
  • 319
3
votes
1 answer

Best way to create a complete readOnly list of object

I'm using C# 4.0, Asp.Net. I have a problem regarding the proper construction of a readonly structure within a custom cache I created. Details (summary) : My CacheManager class (singleton) uses, as parameter, an instance of the existing MemoryCache…
Andy M
  • 5,945
  • 7
  • 51
  • 96