Skip to content

Instantly share code, notes, and snippets.

@nayato
Created July 22, 2016 18:58
Show Gist options
  • Save nayato/b5c7d64f72d5b62adccabd78bd40d615 to your computer and use it in GitHub Desktop.
Save nayato/b5c7d64f72d5b62adccabd78bd40d615 to your computer and use it in GitHub Desktop.
Read-Only List View
[Serializable]
[DebuggerDisplay("Count = {Count}")]
public struct ReadOnlyListView<T> : IReadOnlyList<T>
{
static readonly List<T> EmptyList = new List<T>();
public static ReadOnlyListView<T> Empty => new ReadOnlyListView<T>(EmptyList);
readonly List<T> list;
public ReadOnlyListView(List<T> list)
{
Contract.Requires(list != null);
this.list = list;
}
public int Count => this.list.Count;
public T this[int index] => this.list[index];
public bool Contains(T value) => this.list.Contains(value);
public void CopyTo(T[] array, int index) => this.list.CopyTo(array, index);
public List<T>.Enumerator GetEnumerator() => this.list.GetEnumerator();
IEnumerator<T> IEnumerable<T>.GetEnumerator() => ((IEnumerable<T>)this.list).GetEnumerator();
public int IndexOf(T value) => this.list.IndexOf(value);
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)this.list).GetEnumerator();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment