This project has moved. For the latest updates, please go here.
Supported Platforms
  • .NET 4.0 Client (or newer)
  • Metro
  • Silverlight 3 (or newer)
  • Windows Phone 7 (or newer)
  • Xbox 360
  • Portable class library projects

Library API

The ArraySegments library provides extension methods related to the ArraySegment<T> type.

Converting to/from arrays, and implementing IList<T>:

// Creates an array segment referencing this array.
ArraySegment<T> T[].AsArraySegment(int offset = 0, int count = array.Length - offset);

// Creates a new array containing the elements in this array segment.
T[] ArraySegment<T>.ToArray();

// Copies the elements in this array segment into a destination array.
void ArraySegment<T>.CopyTo(T[] array, int arrayIndex = 0);

// Creates an IList<T> wrapper for this array segment. The wrapper also implements IList.
ArraySegmentListWrapper<T> ArraySegment<T>.AsIList();

Operations:

// Creates a new array segment by taking a number of elements from the beginning of this array segment.
ArraySegment<T> ArraySegment<T>.Take(int count);

// Creates a new array segment by skipping a number of elements from the beginning of this array segment.
ArraySegment<T> ArraySegment<T>.Skip(int count);

// Creates a new array segment by skipping a number of elements and then taking a number of elements from this array segment.
ArraySegment<T> ArraySegment<T>.Slice(int skipCount, int takeCount);

// Creates a new array segment by taking a number of elements from the end of this array segment.
ArraySegment<T> ArraySegment<T>.TakeLast(int count);

// Creates a new array segment by skipping a number of elements from the end of this array segment.
ArraySegment<T> ArraySegment<T>.SkipLast(int count);

// Copies the elements in this array segment into a destination array segment.
void ArraySegment<T>.CopyTo(ArraySegment<T> destination);

Dividing an array segment into other array segments:

ArraySegmentReader<T> ArraySegment<T>.CreateArraySegmentReader();

public sealed class ArraySegmentReader<T>
{
  public ArraySegmentReader(ArraySegment<T> source);
  public ArraySegment<T> Source { get; }
  public int Position { get; set; }
  public int Seek(int offset, SeekOrigin origin);
  public ArraySegment<T> Read(int count);
}

Operations on binary data:

// Creates a MemoryStream over this array segment.
MemoryStream ArraySegment<byte>.CreateStream(bool writable = true);

// Creates a BinaryReader over this array segment.
BinaryReader ArraySegment<byte>.CreateBinaryReader();

// Creates a BinaryWriter over this array segment.
BinaryWriter ArraySegment<byte>.CreateBinaryWriter();

Last edited Jun 8, 2012 at 3:42 PM by StephenCleary, version 3

Comments

No comments yet.