<PackageReference Include="Google.Protobuf" Version="3.35.0" />

Google.Protobuf.UnsafeCollectionOperations

public static class UnsafeCollectionOperations
An unsafe class that provides a set of methods to access the underlying data representations of collections.
public static Span<T> AsSpan<T>(RepeatedField<T> field)

Returns a Span<T> that wraps the current backing array of the given RepeatedField<T>.

Values in the Span<T> should not be set to null. Use Remove or RemoveAt to remove items instead.

The returned Span<T> is only valid until the size of the RepeatedField<T> is modified, after which its state becomes undefined. Modifying existing elements without changing the size is safe as long as the modifications do not set null values.

public static void SetCount<T>(RepeatedField<T> field, int count)

Sets the count of the specified RepeatedField<T> to the given value.

This method should only be called if the subsequent code guarantees to populate the field with the specified number of items.

If count is less than Count, the collection is effectively trimmed down to the first count elements. Capacity is unchanged, meaning the underlying array remains allocated.