Created
July 22, 2016 18:58
-
-
Save nayato/b5c7d64f72d5b62adccabd78bd40d615 to your computer and use it in GitHub Desktop.
Read-Only List View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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