Resolve errors related to inline arrays - C# reference (original) (raw)

This article covers the following compiler errors and warnings:

Inline array declaration

You declare inline arrays as a struct type with a single field, and an attribute that specifies the length of the array. The compiler generates the following errors for invalid inline array declarations:

To fix these arrays, ensure the following are true:

Element access

You access elements of an inline array in the same way as any array. The compiler emits the following errors from incorrect element access:

In addition, the compiler issues the following warning when you declare an indexer:

The generated code for an inline buffer accesses the buffer memory directly, bypassing any declared indexers. Inline arrays can't be used with the foreach statement.

The argument to the indexer must be:

Conversions to Span

You often use System.Span or System.ReadOnlySpan to work with inline arrays. The compiler generates the following errors for invalid conversions:

The compiler generates code that directly accesses the memory for an inline buffer. Therefore, some members aren't ever called. The compiler generates the following warnings if you write one of the members that aren't ever called:

An inline array can be implicitly converted to a Span<T> or ReadOnlySpan<T> to pass an inline array to methods. The compiler enforces restrictions on those conversions:

In addition, the compiler never generates calls to a Slice method in an inline buffer. Conversion operators to convert an inline buffer to a Span or ReadOnlySpan aren't called. The compiler generates code to create a System.Span or System.ReadOnlySpan directly from the memory buffer.