Span and Memory as a replacement for arrays in method signatures?

Viewed 1652

Replace arguments with Span in methods?

Should I replace all my array (such as byte[], char[], and string[]) parameters in my synchronous methods with Span (such as Span<byte>, Span<char>, and Span<string>)?

Example:

public void Foo(byte[] bytes)

With:

public void Foo(Span<byte> bytes)

Replace arguments with Memory in async methods?

Should I replace all my array (such as byte[], char[], and string[]) parameters in my asynchronous methods with Memory (such as Memory<byte>, Memory<char>, and Memory<string>)?

Example:

public async Task FooAsync(byte[] bytes)

With:

public async Task FooAsync(Memory<byte> bytes)

Replace return type with Span in methods?

Should I replace all my array (such as byte[], char[], and string[]) return types in my methods with Span (such as Span<byte>, Span<char>, and Span<string>)?

Example:

public byte[] Foo()

With:

public Span<byte> Foo()

Hmm...

  • Are arrays as method arguments undesired? Is Span favored?
  • Are arrays as return types undesired? Is Span favored?

I've read a couple of articles about Span, ReadOnlySpan and Memory but find it all a bit difficult to grasp for my puny little mind. Is there any rule of thumb or imbeciles guide to Span?

0 Answers
Related