Advertisement
GeneralGDA

ReadOnlyList

Jul 19th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. public sealed class ReadOnlyList<T>
  2. {
  3.     private readonly T[] _backend;
  4.    
  5.     private readonly int _length;
  6.  
  7.     public ReadOnlyList([NotNull] T[] backend)
  8.         :
  9.         this(backend, backend.Length)
  10.     {
  11.     }
  12.  
  13.     public ReadOnlyList([NotNull] T[] backend, int length)
  14.     {
  15.         ThrowIf.Argument.IsNull(backend, nameof(backend));
  16.         ThrowIf.Argument.OutOfRange(length, 0, backend.Length, nameof(length));
  17.  
  18.         _backend = backend;
  19.        
  20.         _length = length;
  21.     }
  22.  
  23.     public ReadOnlyList([NotNull] IEnumerable<T> source)
  24.     {
  25.         ThrowIf.Argument.IsNull(source, nameof(source));
  26.  
  27.         _backend = source.ToArray();
  28.  
  29.         _length = _backend.Length;
  30.     }
  31.  
  32.     [Pure]
  33.     public ArrayEnumerator<T> GetEnumerator()
  34.     {
  35.         return new ArrayEnumerator<T>(_backend, _length);
  36.     }
  37.  
  38.     [Pure]
  39.     public ReadOnlySpan<T> Access()
  40.     {
  41.         return _backend.AsSpan(0, _length);
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement