lucidquiet
lucidquiet

Reputation: 6558

Is there a set of C# data readonly generic collections / data structures?

I'm looking for a library of read-only generic collections that are a mirror (or rather implement or extend) the .NET versions of the common System.Collections.Generic; collections compatible with .NET v3.5.

I would guess that F# probably has a number readonly collections (but I'm not sure if they implement the interfaces from System.Collections.Generic). Beyond that I'd rather not install F# on servers. I guess I might be able to simply deploy the F# dll(s), if that's doable, which I suspect it is.

Is there a library of read-only generic collection classes that implement the interfaces in System.Collections.Generic and provide the same performance (or better) characteristics? For instance, random access array backed implementation of IList (only read-only).

Upvotes: 0

Views: 812

Answers (4)

Eren Ersönmez
Eren Ersönmez

Reputation: 39085

Depending on your requirements, it might be sufficient to expose the collection as IEnumerable<T>, which is read-only by nature:

var list = new List<int> {1,2,3,4};
var coll = list.AsEnumerable(); //read-only interface

However, the caller could still cast the collection to the concrete type to make it mutable:

var backToList = (List<int>)coll; //now it's mutable again

You also have the option to use a ReadOnlyCollection<T>, which doesn't have the problem above:

var readonlyCollection = list.AsReadOnly(); // returns a ReadOnlyCollection<int>

Upvotes: 0

C&#233;dric Bignon
C&#233;dric Bignon

Reputation: 13022

The ReadOnlyCollection<T> in System.Collections.ObjectModel

It is a wrapper on any collection you want. The most common is to use a List<T> and wrap it with the ReadOnlyCollection<T>.

Then, in the public implementation, you only give access to ReadOnlyCollection<T> and set List<T> as private.

public class MyClass
{
    private List<MyItemClass> list;
    public ReadOnlyCollection<MyItemClass> MyReadOnlyCollection { get; private set; }

    public MyClass()
    {
        list = new List<MyItemClass>() { ... };
        MyReadOnlyCollection = new ReadOnlyCollection<MyItemClass>(list);
    }
}

If you want an "immutable object":

public class MyClass
{
    public ReadOnlyCollection<MyItemClass> MyReadOnlyCollection { get; private set; }

    public MyClass()
    {
        List<MyItemClass> list = new List<MyItemClass>() { ... };
        MyReadOnlyCollection = new ReadOnlyCollection<MyItemClass>(list);
    }
}

This way, the state of MyReadOnlyCollection and of list will not be changed.

Upvotes: 3

Daniel
Daniel

Reputation: 47904

If you want immutable, not just read-only, collections you might be interested in Microsoft's preview of immutable collections.

There are counterparts for most of the classes in System.Collections.Generic.

Upvotes: 6

Henry
Henry

Reputation: 56

You should take a look at System.Collection.ObjectModel.

There you got a read-only dictionary, list and observable list.

They play nice with anything enumerable.

Upvotes: 0

Related Questions