In this article two Simple Implementations of ReadOnlyDictionary<TKey,TValue> and ReadOnlyList<T>.
ReadOnlyDictionary<TKey,TValue>
public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
#region Private Fields
private IDictionary<TKey, TValue> _original;
#endregion
#region Constructors
public ReadOnlyDictionary(IDictionary<TKey, TValue> original)
{
_original = original;
}
#endregion
#region IDictionary<TKey,TValue> Members
public void Add(TKey key, TValue value)
{
throw new NotSupportedException();
}
public bool ContainsKey(TKey key)
{
return _original.ContainsKey(key);
}
public ICollection<TKey> Keys
{
get { return _original.Keys; }
}
public bool Remove(TKey key)
{
throw new NotSupportedException();
}
public bool TryGetValue(TKey key, out TValue value)
{
return _original.TryGetValue(key, out value);
}
public ICollection<TValue> Values
{
get { return _original.Values; }
}
public TValue this[TKey key]
{
get
{
return _original[key];
}
set
{
throw new NotSupportedException();
}
}
#endregion
#region ICollection<KeyValuePair<TKey,TValue>> Members
public void Add(KeyValuePair<TKey, TValue> item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return _original.Contains(item);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
_original.CopyTo(array, arrayIndex);
}
public int Count
{
get { return _original.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
throw new NotSupportedException();
}
#endregion
#region IEnumerable<KeyValuePair<TKey,TValue>> Members
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _original.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _original.GetEnumerator();
}
#endregion
}
ReadOnlyList<T>
public class ReadOnlyList<T> : IList<T>
{
#region Private Fields
private IList<T> _original;
#endregion
#region Constructors
public ReadOnlyList(IList<T> original)
{
_original = original;
}
#endregion
#region IList<T> Members
public int IndexOf(T item)
{
return _original.IndexOf(item);
}
public void Insert(int index, T item)
{
throw new NotSupportedException();
}
public void RemoveAt(int index)
{
throw new NotSupportedException();
}
public T this[int index]
{
get
{
return _original[index];
}
set
{
throw new NotSupportedException();
}
}
#endregion
#region ICollection<T> Members
public void Add(T item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Contains(T item)
{
return _original.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_original.CopyTo(array, arrayIndex);
}
public int Count
{
get { return _original.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public bool Remove(T item)
{
throw new NotSupportedException();
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
return _original.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _original.GetEnumerator();
}
#endregion
}
Hope this help’s!
Views(992)

