Compatibility.

This class contains a set of static methods that allows you to easily treat any Microsoft .NET Framework Library traditional collection interface instance as its similar read only collection interface version and vice versa. That way you can get an object wrapper that implements IReadOnlyCollection<T> receiving an ICollection<T> object, and the same way does with IList<T> and IDictionary<TKey, TValue> interfaces. Each method returns a wrapper for the specified collection that implements both the read-only collection interface and its similar collection interface. Next table shows more details about the methods in this class:

Method return type Method name Method parameter type
IReadOnlyCollection<T> AsReadOnlyCollection ICollection<T>
IReadOnlyList<T> AsReadOnlyList IList<T>
IReadOnlyDictionary<TKey, TValue> AsReadOnlyDictionary IDictionary<TKey, TValue>
ICollection<T> AsCollection IReadOnlyCollection<T>
IList<T> AsList IReadOnlyList<T>
IDictionary<TKey, TValue> AsDictionary IReadOnlyDictionary<TKey, TValue>

Lets assume we have an ICollection<T> implementation and some class with some method that receives an IReadOnlyCollection<T> by parameter.

The ICollection<T> implementation:

//A collection that implements 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();
}

Some class with some method

public static class SomeClass<T>
{
    public static void SomeMethod(IReadOnlyCollection<T> collection);
}

If we want to pass an instance of MyCollection<T> class to SomeMethod as parameter the code is simple as this:

public class Program
{
    static void Main(string[] args)
    {
        //Initializing a new instance of MyCollection<T> class.
        MyCollection<string> collection = new MyCollection<string>();

        //Getting the collection as IReadOnlyCollection<string>.
        IReadOnlyCollection<string> readOnlyCollection = Compatibility.AsReadOnlyCollection<string>(collection);
        //Calling the method.
        SomeClass<string>.SomeMethod(readOnlyCollection);
    }
}

if we check collection and readOnlyCollection variables we can verify that they represent different instances, that's why as MyCollection<T> does not implement IReadOnlyCollection<T> the Compatibility class uses a wrapper that contains the specified instance as an underlying object and implements both ICollection<T> and IReadOnlyCollection<T>. If the specified IReaOnlyCollection<T> instance also implements ICollection<T> the Compatibility class methods returns the same object. Next example shows this is code:

//A collection that implements ICollection<T> and IReadOnlyCollection<T>.
public class MyCompatibleCollection<T> : ICollection<T>, IReadOnlyCollection<T>
{
    public MyCompatibleCollection();

    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 instantiate both MyCollection<T> and MyCompatibilityCollection<T>, pass them as parameter to the AsReadOnlyCollection method in the Compatibility class and compare the result instances to the originals we have this results:

public class Program
{
    static void Main(string[] args)
    {
        MyCollection<string> collection = new MyCollection<string>();
        IReadOnlyCollection<string> readOnlyCollection1 = Compatibility.AsReadOnlyCollection<string>(collection);
        Console.WriteLine(collection.Equals(readOnlyCollection1));

        MyCompatibleCollection<string> compatibleCollection = new MyCompatibleCollection<string>();
        IReadOnlyCollection<string> readOnlyCollection2 = Compatibility.AsReadOnlyCollection<string>(compatibleCollection);
        Console.WriteLine(compatibleCollection.Equals(readOnlyCollection2));
    }
}

Output:
False
True
See Also
Learn more

Last edited Nov 25, 2013 at 5:38 AM by rathmA, version 1

Comments

No comments yet.