Microsoft .NET Framework 4.5 new Collection Interfaces.

System.Collections and System.Collections.Generic are the main namespaces for data structures in the Microsoft .NET Framework Base Class Library. Inside those namespaces is contained all the foundations and features needed to use or implement collections in this platform.
Microsoft .NET Framework 4.5 expands the interfaces that represents those foundations by presenting a set of new read only collection interfaces: IReadOnlyCollection<T>, IReadOnlyList<T> and IReadOnlyDictionary<TKey, TValue>. This new interfaces has been created by extracting some read only properties and methods from the traditional collection interfaces. In order to preserve integration, the Microsoft .NET Framework 4.5 also presents new implementation of its traditional collections with total compatibility with the new updated platform.
If you have your own collection and you want to update it so it implements the new interfaces the only thing you have to do is specify the dependency in code. In the next example we have an implementation of ICollection<T>.

public class MyCollection<T> : ICollection<T>
{
    public MyCollection();

    public int Count { get; }
    public bool IsReadOnly { get; }

    public void Add(T item);
    public void Clear();
    public bool Contains(T item);
    public void CopyTo(T[] array, int arrayIndex);
    public bool Remove(T item);
    public IEnumerator<T> GetEnumerator();
}

If we want this collection implements IReadOnlyCollection<T> the only update in code you have to do is the implementation specification, the new code should look like this:

public class MyCollection<T> : ICollection<T>, IReadOnlyCollection<T>
{
    …
}

As IReadOnlyCollection<T> members are contained in ICollection<T>, then all the members are already implemented.
That case is very easy, but what happens if the collection is contained in a library extern to the Microsoft .NET Framework Library that you can no access the code and you have to use that collection as IReadOnlyCollection<T>. If the developer releases a new version of the library that updates the contained collections the problem is solved, otherwise you have to develop your own version of the collection. Academy.Collections presents a set of static methods that allows conversions between the old and the new interfaces, providing a fast and efficient way to integrate your code and referenced code with the new read only collection interfaces. For more information see Compatibility.
Next.
Learn more.

Last edited Nov 25, 2013 at 4:35 AM by rathmA, version 1

Comments

No comments yet.