readonlycollection

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<Foo> foos; // Which one is to be preferred? public IEnumerable<Foo> Foos { ... } public ReadOnlyCollection<Foo> Foos { ... } } /...

object[] from ReadOnlyCollection<T>

I have an object that I ended up with via a reflection call: object readOnlyCollectionObject = propertyInfo.GetValue(someEntity, null); I know this object is a generic ReadOnlycollection. It could be a ReadOnlyCollection<Cat>, ReadOnlyCollection<Dog>, etc. For argument sake, lets just say it is a ReadOnlyCollection<T>. Even though a D...

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 & butter. The problem is that the List<> interface is much richer exposing all sorts of u...

unmodifiablelist in .NET 4.0

From what I can tell, .NET 4.0 still lacks readonly lists. Can anyone shed light on why the framework still lacks this functionality? Isn't this one of the commonest pieces of functionality for domain drive design? One of the few advantages Java has over C# is this in the form of the Collections.unmodifiablelist(list) method, which it s...

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<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 th...

Is there anything magic about ReadOnlyCollection

Having this code... var b = new ReadOnlyCollection<int>(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<T> implements IList<T> and the this[T] have a setter in the IList<T> interface. I've tried to replicate the functionality of ReadOnlyCollection, bu...

BindingSource / BindingNavigator: How to prevent editing of bound DataSource?

I created a Data Source with VB.NET and Visual Studio 2005. I dragged the data source onto my dialog, and VS created the text boxes with the members of my linked Object, a System.Windows.Forms.BindingSource and a System.Windows.Forms.BindingNavigator. I populate the List (called myList), set myList as the DataSource in the BindingSourc...

Does AutoMapper support ReadOnlyCollections?

I have tried various configurations, but cannot get it working. I am wondering if it is supported? ...

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

The IList<T> interface includes access by index in addition to operations not supported by the SortedList<TKey, TValue>.Keys property such as Add, Remove, and Insert. A ReadOnlyCollection<T>, such as the return value of List<T>.AsReadOnly, implements IList<T> and therefore offers access by index but hides illegal operations like Add, et...

how to make accessor for Dictionary in a way that returned Dictionary cannot be changed C# / 2.0

I thought of solution below because the collection is very very small. But what if it was big? private Dictionary<string, OfTable> _folderData = new Dictionary<string, OfTable>(); public Dictionary<string, OfTable> FolderData { get { return new Dictionary<string,OfTable>(_folderData); } } With List you can make: public class...

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 Violations suggest i clone the object (which i don't want to do) or 'Replace the array with...

FxCop CA2227 warning and ReadOnlyCollection<T>

In my VS2008 SP1, .NET 3.5 SP1 project, I have different classes that contain different properties. I use C#3.0 auto properties a lot. Some of these properties need to be collections. Since I want to make it simple, I use ReadOnlyCollection<T> for these properties. I don't want to use IEnumerable<T> since I want random access to the el...

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? ...

what's the alternative to readonlycollection when using lazy="extra"?

I am trying to use lazy="extra" for the child collection of Trades I have on my Client object. The trades is an Iset<Trade> Trades, exposed as ReadOnlyCollection<Trade> because I do not want anyone to modify the collection directly. As a result, I have added AddTrade and RemoveTrade methods. Now I have a Client Search page where I need ...

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 = data[0]; // only reading should be allowed ... } Is there a way of readon...

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 am using. Can you please tell me if there are better ways to do this common thing? It all ...

Readonly collection properties that NHibernate can work with

Hi all My domain classes have collections that look like this: private List<Foo> _foos = new List<Foo>(); public virtual ReadOnlyCollection<Foo> Foos { get { return _foos.AsReadOnly(); } } This gives me readonly collections that can be modified from within the class (i.e. by using the field _foos). This collection is mapped as follo...

C#, SynchronizedReadOnlyCollection and its constructors

.net class SynchronizedReadOnlyCollection has 4 constructors. public SynchronizedReadOnlyCollection(); public SynchronizedReadOnlyCollection(object syncRoot); public SynchronizedReadOnlyCollection(object syncRoot, IEnumerable<T> list); public SynchronizedReadOnlyCollection(object syncRoot, params T[] list); What is the...

NHibernate: use of IEnumerable as collection type results in error

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 propert...