using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public static class Program
public static void Main()
var dnaGenerator = new DnaGenerator();
Console.WriteLine($"TotalMemory: {GC.GetTotalMemory(true):#,0} bytes");
Console.WriteLine($"TotalMemory: {GC.GetTotalMemory(true):#,0} bytes");
GC.KeepAlive(dnaGenerator);
static void DoWork(DnaGenerator dnaGenerator)
foreach (string dna in dnaGenerator.Take(5))
Console.WriteLine($"Processing DNA of {dna.Length:#,0} nucleotides, starting from {dna[0]}");
class DnaGenerator : IEnumerable<string>
private readonly IEnumerable<string> _enumerable;
public DnaGenerator() => _enumerable = Iterator().Unwrap();
private IEnumerable<StrongBox<string>> Iterator()
foreach (char c in new char[] { 'A', 'C', 'G', 'T' })
yield return new(new String(c, 10_000_000));
public IEnumerator<string> GetEnumerator() => _enumerable.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public static IEnumerable<T> Unwrap<T>(this IEnumerable<StrongBox<T>> source)
=> new StrongBoxUnwrapper<T>(source);
private class StrongBoxUnwrapper<T> : IEnumerable<T>
private readonly IEnumerable<StrongBox<T>> _source;
public StrongBoxUnwrapper(IEnumerable<StrongBox<T>> source)
ArgumentNullException.ThrowIfNull(source);
public IEnumerator<T> GetEnumerator() => new Enumerator(_source.GetEnumerator());
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private class Enumerator : IEnumerator<T>
private readonly IEnumerator<StrongBox<T>> _source;
private StrongBox<T> _latest;
public Enumerator(IEnumerator<StrongBox<T>> source)
ArgumentNullException.ThrowIfNull(source);
public T Current => _source.Current.Value;
object IEnumerator.Current => Current;
var moved = _source.MoveNext();
_latest = _source.Current;
if (_latest is not null) _latest.Value = default;
public void Reset() => _source.Reset();