2
0

ViewCollectionHelpers.cs 741 B

1234567891011121314151617181920
  1. namespace Terminal.Gui.ViewBase;
  2. internal static class ViewCollectionHelpers
  3. {
  4. /// <summary>Returns a defensive copy of any <see cref="IEnumerable{T}"/>.</summary>
  5. internal static View [] Snapshot (this IEnumerable<View> source)
  6. {
  7. if (source is IList<View> list)
  8. {
  9. // The list parameter might be the live `_subviews`, so freeze it under a lock
  10. lock (list)
  11. {
  12. return list.ToArray (); // It’s slightly less “fancy C# 12”, but much safer in multithreaded code
  13. }
  14. }
  15. // Anything else (LINQ result, iterator block, etc.) we just enumerate.
  16. return source.ToArray (); // Safe because it’s not shared mutable state
  17. }
  18. }