From e183c31579382d35801774b2f8f6ba4fefa06146 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 20 Apr 2022 19:24:42 +0100 Subject: [PATCH] Add Array.AsReadOnly and Array.Clear Tests were added, implementation was missing --- X10D/src/ArrayExtensions/ArrayExtensions.cs | 89 +++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 X10D/src/ArrayExtensions/ArrayExtensions.cs diff --git a/X10D/src/ArrayExtensions/ArrayExtensions.cs b/X10D/src/ArrayExtensions/ArrayExtensions.cs new file mode 100644 index 0000000..6f454dc --- /dev/null +++ b/X10D/src/ArrayExtensions/ArrayExtensions.cs @@ -0,0 +1,89 @@ +namespace X10D; + +/// +/// Extension methods for . +/// +public static class ArrayExtensions +{ + /// + /// Returns a read-only wrapper for the array. + /// + /// The one-dimensional, zero-based array to wrap in a read-only wrapper. + /// The type of the elements in the array. + /// A wrapper for the specified array. + /// is . + public static IReadOnlyCollection AsReadOnly(this T[] array) + { + if (array is null) + { + throw new ArgumentNullException(nameof(array)); + } + + return Array.AsReadOnly(array); + } + + /// + /// Clears the contents of an array. + /// + /// The array to clear. + /// The type of the elements in the array. + /// is . + public static void Clear(this T?[] array) + { + array.Clear(..); + } + + /// + /// Sets a range of elements in an array to the default value of each element type. + /// + /// The array whose elements need to be cleared. + /// A range defining the start index and number of elements to clear. + /// The type of the elements in the array. + /// is . + public static void Clear(this T?[] array, Range range) + { + if (array is null) + { + throw new ArgumentNullException(nameof(array)); + } + + int index = range.Start.Value; + int end = range.End.Value; + if (range.End.IsFromEnd) + { + end = array.Length - end; + } + + array.Clear(index, end - index); + } + + /// + /// Sets a range of elements in an array to the default value of each element type. + /// + /// The array whose elements need to be cleared. + /// The starting index of the range of elements to clear. + /// The number of elements to clear. + /// The type of the elements in the array. + /// is . + /// + /// is less than the lower bound of . + /// -or- + /// is less zero. + /// -or- + /// The sum of and is greater than the size of array. + /// + public static void Clear(this T?[] array, int index, int length) + { + if (array is null) + { + throw new ArgumentNullException(nameof(array)); + } + + if (length == 0 || array.Length == 0) + { + return; + } + + Array.Clear(array, index, length); + } +}