Skip to content

ArrayMethods

Jordan Duerksen edited this page Apr 2, 2018 · 2 revisions

ArrayMethods provides extra functionality for working with arrays.

using W;

    private byte[] complete = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Peek methods do not change the source array

    // return the first 5 elements from the array
    var elements = ArrayMethods.PeekStart(complete, 5);

    // return 5 elements from the array, starting at index 3
    var elements = ArrayMethods.Peek(complete, 3, 5);

    // return the last 5 elements from the array
    var elements = ArrayMethods.PeekEnd(complete, 5);

Take methods remove items from the source array

    // take the first 5 elements from the array
    var elements = ArrayMethods.TakeFromStart(ref complete, 5);

    // take 5 elements from the array, starting at index 3
    var elements = ArrayMethods.Take(ref complete, 3, 5);

    // take the last 5 elements from the array
    var elements = ArrayMethods.TakeFromEnd(ref complete, 5);

Trim methods are similar to the Take methods, except they return the source array instead of the elements removed

    // trim the first 5 elements from the array
    var trimmed = ArrayMethods.TrimStart(ref complete, 5);

    // trim 5 elements from the array, starting at index 3
    var trimmed = ArrayMethods.Trim(ref complete, 3, 5);

    // trim the last 5 elements from the array
    var trimmed = ArrayMethods.TrimEnd(ref complete, 5);

Append adds new elements to the end of the source array. The source array is resized to accommodate the additions.

    var data = new byte[] { 0, 1, 2, 3, 4 };
    var complete = ArrayMethods.Append(ref data, new byte[] { 5, 6, 7, 8, 9 });

Insert adds new elements beginning at the specified start index. The source array is resized to accommodate the additions.

    var data = new byte[] { 0, 1, 2, 3, 6, 7, 8, 9 };
    var result = ArrayMethods.Insert(ref data, new byte[] { 4, 5 }, 4);

Clone this wiki locally