How is the new C# Span different from ArraySegment?

前端 未结 3 1795
清酒与你
清酒与你 2021-02-06 21:50

I am having trouble conceptualizing the usages for the new Span in C#.

  1. What construct(s) does it replace? Is ArraySegment now obsolete?

  2. What fu

3条回答
  •  伪装坚强ぢ
    2021-02-06 22:23

    From MSDN Magazine: Span is defined in such a way that operations can be as efficient as on arrays: indexing into a span doesn’t require computation to determine the beginning from a pointer and its starting offset, as the ref field itself already encapsulates both. (By contrast, ArraySegment has a separate offset field, making it more expensive both to index into and to pass around.)

    Also, while ArraySegment implements IEnumerable, Span doesn't.

提交回复
热议问题