/// <summary>
        /// Creates an array from the contents of this builder.
        /// </summary>
        /// <remarks>
        /// Regions created with <see cref="Reserve"/> will be default-initialized.
        /// </remarks>
        public T[] ToArray()
        {
            // If no regions were reserved, there are no 'gaps' we need to add to the array.
            // In that case, we can just call ToArray on the underlying builder.
            if (_markers.Count == 0)
            {
                Debug.Assert(_reservedCount == 0);
                return(_builder.ToArray());
            }

            var array = new T[Count];

            CopyTo(array, 0, array.Length);
            return(array);
        }
        /// <summary>Converts an enumerable to an array.</summary>
        /// <param name="source">The enumerable to convert.</param>
        /// <returns>The resulting array.</returns>
        internal static T[] ToArray <T>(IEnumerable <T> source)
        {
            Debug.Assert(source != null);

            if (source is ICollection <T> collection)
            {
                int count = collection.Count;
                if (count == 0)
                {
                    return(Array.Empty <T>());
                }

                var result = new T[count];
                collection.CopyTo(result, arrayIndex: 0);
                return(result);
            }

            var builder = new LargeArrayBuilder <T>(initialize: true);

            builder.AddRange(source);
            return(builder.ToArray());
        }