using System.Collections;
using System.Collections.Generic;
public static void Main()
var arr = SomeMagicalArrayWrapper.Create(new byte[][][]
new byte[][] { new byte[] { 1, 2 }, new byte[] { 3, 4 } },
new byte[][] { new byte[] { 5 }, new byte[] { 6, 7, 8 } }
for (int i = 0; i < arr.Count; i++)
Console.WriteLine(arr[i]);
public static class SomeMagicalArrayWrapper
public static SomeMagicalArrayWrapper<T> Create<T>(T[][] array) => new SomeMagicalArrayWrapper<T>(array);
public static SomeMagicalArrayWrapper<T> Create<T>(T[][][] array)
var outer = new IList<T>[array.Length];
for (int i = 0; i < array.Length; i++)
outer[i] = new SomeMagicalArrayWrapper<T>(array[i]);
return new SomeMagicalArrayWrapper<T>(outer);
public static SomeMagicalArrayWrapper<T> Create<T>(T[][][][] array)
var outer = new IList<T>[array.Length];
for (int i = 0; i < array.Length; i++)
outer[i] = Create<T>(array[i]);
return new SomeMagicalArrayWrapper<T>(outer);
public struct SomeMagicalArrayWrapper<T> : IList<T>
private readonly IList<IList<T>> lists;
private readonly int[] listStarts;
public int Count { get; }
public SomeMagicalArrayWrapper(IList<IList<T>> lists)
ArgumentNullException.ThrowIfNull(lists);
listStarts = new int[lists.Count];
for (int i = 0; i < lists.Count; i++)
throw new ArgumentNullException($"lists[{i}]");
listStarts[i] = listStart;
listStart += lists[i].Count;
var (listIndex, elementIndex) = GetIndexes(index);
return lists[listIndex][elementIndex];
var (listIndex, elementIndex) = GetIndexes(index);
lists[listIndex][elementIndex] = value;
private (int, int) GetIndexes(int index)
int arrayIndex = listStarts.AsSpan().BinarySearch(index);
arrayIndex = ~arrayIndex - 1;
return (arrayIndex, index - listStarts[arrayIndex]);
public IEnumerator<T> GetEnumerator()
foreach (var list in lists)
foreach (T element in list)
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
int IList<T>.IndexOf(T element)
throw new NotSupportedException();
bool ICollection<T>.Contains(T element)
throw new NotSupportedException();
void ICollection<T>.CopyTo(T[] dest, int offset)
throw new NotSupportedException();
bool ICollection<T>.IsReadOnly => false;
void IList<T>.Insert(int index, T item) => throw new NotSupportedException("Collection is fixed-length");
void IList<T>.RemoveAt(int index) => throw new NotSupportedException("Collection is fixed-length");
void ICollection<T>.Add(T item) => throw new NotSupportedException("Collection is fixed-length");
bool ICollection<T>.Remove(T item) => throw new NotSupportedException("Collection is fixed-length");
void ICollection<T>.Clear() => throw new NotSupportedException("Collection is fixed-length");