ReadOnlyCollection.cs 1014 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace InitialPrefabs.NimGui.Collections {
  5. public struct ReadOnlyCollection<T> : IEnumerable<T>, IEnumerable {
  6. readonly List<T> Collection;
  7. public int Count => Collection.Count;
  8. public T this[int index] => Collection[index];
  9. public ReadOnlyCollection(List<T> source) {
  10. Collection = source;
  11. }
  12. public List<T>.Enumerator GetEnumerator() {
  13. return Collection.GetEnumerator();
  14. }
  15. public bool Contains(T item) {
  16. return Collection.Contains(item);
  17. }
  18. IEnumerator<T> IEnumerable<T>.GetEnumerator() {
  19. throw new NotSupportedException("To avoid boxing, do not cast NoAllocReadOnlyCollection to IEnumerable<T>.");
  20. }
  21. IEnumerator IEnumerable.GetEnumerator() {
  22. throw new NotSupportedException("To avoid boxing, do not cast NoAllocReadOnlyCollection to IEnumerable.");
  23. }
  24. }
  25. }