Span<T>
instead of T[]
when possible to reduce allocations CS-P1011Span<T>
is a structure that is allocated on the stack rather than heap, thereby reducing the number of allocations during the program's lifetime which in return exerts less pressure on the GC. Since the method that you're trying to invoke has an overload that accepts Span<T>
, it is therefore suggested that you use that overload and pass Span<T>
rather than T[]
.
// Implementation of `Foo()`
public void Foo(Span<T> s)
{
//
}
// Calling `Foo()`
Foo(arr[1..3]);
// Implementation of `Foo()`
public void Foo(Span<T> s)
{
//
}
// Calling `Foo()`
Foo(arr.AsSpan()[1..3]);