diff --git a/src/Foundation/NSCollectionLinqExtensions.cs b/src/Foundation/NSCollectionLinqExtensions.cs new file mode 100644 index 000000000000..b60d8920dd77 --- /dev/null +++ b/src/Foundation/NSCollectionLinqExtensions.cs @@ -0,0 +1,2893 @@ +using System.Collections.Generic; + +#nullable enable + +namespace System.Linq { + public static class NSCollectionLinqExtensions { + // NSSet + /// Returns the first element of . + /// The element type of the collection. + /// The source collection. + /// The first element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey First (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.First ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey First (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.First ((IEnumerable) source, predicate); + } + + /// Returns the first element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The first element in , or if the collection is empty. + /// Thrown when is . + public static TKey? FirstOrDefault (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.FirstOrDefault ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? FirstOrDefault (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.FirstOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the last element of . + /// The element type of the collection. + /// The source collection. + /// The last element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey Last (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Last ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey Last (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Last ((IEnumerable) source, predicate); + } + + /// Returns the last element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The last element in , or if the collection is empty. + /// Thrown when is . + public static TKey? LastOrDefault (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LastOrDefault ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? LastOrDefault (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LastOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the only element of . + /// The element type of the collection. + /// The source collection. + /// The single element of . + /// Thrown when is . + /// Thrown when is empty or contains more than one element. + public static TKey Single (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Single ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element or more than one element satisfies the condition in . + public static TKey Single (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Single ((IEnumerable) source, predicate); + } + + /// Returns the only element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The single element of , or if the collection is empty. + /// Thrown when is . + /// Thrown when contains more than one element. + public static TKey? SingleOrDefault (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.SingleOrDefault ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in , or if no such element is found. + /// Thrown when or is . + /// Thrown when more than one element satisfies the condition in . + public static TKey? SingleOrDefault (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SingleOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the element at a specified index in . + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in . + /// Thrown when is . + /// Thrown when is less than 0 or greater than or equal to the number of elements in . + public static TKey ElementAt (this Foundation.NSSet source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAt ((IEnumerable) source, index); + } + + /// Returns the element at a specified index in , or if the index is out of range. + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in , or if the index is out of range. + /// Thrown when is . + public static TKey? ElementAtOrDefault (this Foundation.NSSet source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAtOrDefault ((IEnumerable) source, index); + } + + /// Determines whether contains any elements. + /// The element type of the collection. + /// The source collection. + /// if contains any elements; otherwise . + /// Thrown when is . + public static bool Any (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Any ((IEnumerable) source); + } + + /// Determines whether any element of satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if any element in passes the test in ; otherwise . + /// Thrown when or is . + public static bool Any (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Any ((IEnumerable) source, predicate); + } + + /// Determines whether all elements of satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if every element in passes the test in , or if the collection is empty; otherwise . + /// Thrown when or is . + public static bool All (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.All ((IEnumerable) source, predicate); + } + + /// Returns the number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static int Count (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Count ((IEnumerable) source); + } + + /// Returns the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static int Count (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Count ((IEnumerable) source, predicate); + } + + /// Returns a that represents the total number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static long LongCount (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LongCount ((IEnumerable) source); + } + + /// Returns a that represents the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static long LongCount (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LongCount ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate that incorporates each element's index. + /// The element type of the collection. + /// The source collection. + /// A function to test each element; the second parameter represents the zero-based index of the element. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Projects each element of into a new form. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSSet source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Projects each element of into a new form by incorporating each element's index. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element; the second parameter represents the zero-based index of the element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSSet source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Sorts the elements of in ascending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderBy (this Foundation.NSSet source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderBy ((IEnumerable) source, keySelector); + } + + /// Sorts the elements of in descending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted in descending order according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderByDescending (this Foundation.NSSet source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderByDescending ((IEnumerable) source, keySelector); + } + + /// Bypasses a specified number of elements in and returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// The number of elements to skip before returning the remaining elements. + /// An that contains elements after the skipped ones. + /// Thrown when is . + public static IEnumerable Skip (this Foundation.NSSet source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Skip ((IEnumerable) source, count); + } + + /// Bypasses elements in as long as a condition is true, then returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains the elements starting at the first element that does not satisfy the condition. + /// Thrown when or is . + public static IEnumerable SkipWhile (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SkipWhile ((IEnumerable) source, predicate); + } + + /// Returns a specified number of contiguous elements from the start of . + /// The element type of the collection. + /// The source collection. + /// The number of elements to return. + /// An that contains the specified number of elements from the start of . + /// Thrown when is . + public static IEnumerable Take (this Foundation.NSSet source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Take ((IEnumerable) source, count); + } + + /// Returns elements from as long as a specified condition is true. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from as long as the condition is true. + /// Thrown when or is . + public static IEnumerable TakeWhile (this Foundation.NSSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.TakeWhile ((IEnumerable) source, predicate); + } + + /// Returns distinct elements from . + /// The element type of the collection. + /// The source collection. + /// An that contains distinct elements from . + /// Thrown when is . + public static IEnumerable Distinct (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Distinct ((IEnumerable) source); + } + + /// Inverts the order of the elements in . + /// The element type of the collection. + /// The source collection. + /// An whose elements correspond to those of in reverse order. + /// Thrown when is . + public static IEnumerable Reverse (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Reverse ((IEnumerable) source); + } + + /// Concatenates with another sequence. + /// The element type of the collection. + /// The source collection. + /// The sequence to concatenate to . + /// An that contains the concatenated elements of the two sequences. + /// Thrown when or is . + public static IEnumerable Concat (this Foundation.NSSet source, IEnumerable second) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (second); + return Enumerable.Concat ((IEnumerable) source, second); + } + + /// Creates a from . + /// The element type of the collection. + /// The source collection. + /// A that contains elements from . + /// Thrown when is . + public static List ToList (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToList ((IEnumerable) source); + } + + /// Creates an array from . + /// The element type of the collection. + /// The source collection. + /// An array that contains elements from . + /// Thrown when is . + public static TKey [] ToArray (this Foundation.NSSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToArray ((IEnumerable) source); + } + + /// Applies an accumulator function over . + /// The element type of the collection. + /// The source collection. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + /// Thrown when is empty. + public static TKey Aggregate (this Foundation.NSSet source, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, func); + } + + /// Applies an accumulator function over with a seed value. + /// The element type of the collection. + /// The type of the accumulator value. + /// The source collection. + /// The initial accumulator value. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + public static TAccumulate Aggregate (this Foundation.NSSet source, TAccumulate seed, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, seed, func); + } + + // NSMutableSet + /// Returns the first element of . + /// The element type of the collection. + /// The source collection. + /// The first element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey First (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.First ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey First (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.First ((IEnumerable) source, predicate); + } + + /// Returns the first element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The first element in , or if the collection is empty. + /// Thrown when is . + public static TKey? FirstOrDefault (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.FirstOrDefault ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? FirstOrDefault (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.FirstOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the last element of . + /// The element type of the collection. + /// The source collection. + /// The last element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey Last (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Last ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey Last (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Last ((IEnumerable) source, predicate); + } + + /// Returns the last element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The last element in , or if the collection is empty. + /// Thrown when is . + public static TKey? LastOrDefault (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LastOrDefault ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? LastOrDefault (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LastOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the only element of . + /// The element type of the collection. + /// The source collection. + /// The single element of . + /// Thrown when is . + /// Thrown when is empty or contains more than one element. + public static TKey Single (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Single ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element or more than one element satisfies the condition in . + public static TKey Single (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Single ((IEnumerable) source, predicate); + } + + /// Returns the only element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The single element of , or if the collection is empty. + /// Thrown when is . + /// Thrown when contains more than one element. + public static TKey? SingleOrDefault (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.SingleOrDefault ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in , or if no such element is found. + /// Thrown when or is . + /// Thrown when more than one element satisfies the condition in . + public static TKey? SingleOrDefault (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SingleOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the element at a specified index in . + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in . + /// Thrown when is . + /// Thrown when is less than 0 or greater than or equal to the number of elements in . + public static TKey ElementAt (this Foundation.NSMutableSet source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAt ((IEnumerable) source, index); + } + + /// Returns the element at a specified index in , or if the index is out of range. + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in , or if the index is out of range. + /// Thrown when is . + public static TKey? ElementAtOrDefault (this Foundation.NSMutableSet source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAtOrDefault ((IEnumerable) source, index); + } + + /// Determines whether contains any elements. + /// The element type of the collection. + /// The source collection. + /// if contains any elements; otherwise . + /// Thrown when is . + public static bool Any (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Any ((IEnumerable) source); + } + + /// Determines whether any element of satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if any element in passes the test in ; otherwise . + /// Thrown when or is . + public static bool Any (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Any ((IEnumerable) source, predicate); + } + + /// Determines whether all elements of satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if every element in passes the test in , or if the collection is empty; otherwise . + /// Thrown when or is . + public static bool All (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.All ((IEnumerable) source, predicate); + } + + /// Returns the number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static int Count (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Count ((IEnumerable) source); + } + + /// Returns the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static int Count (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Count ((IEnumerable) source, predicate); + } + + /// Returns a that represents the total number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static long LongCount (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LongCount ((IEnumerable) source); + } + + /// Returns a that represents the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static long LongCount (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LongCount ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate that incorporates each element's index. + /// The element type of the collection. + /// The source collection. + /// A function to test each element; the second parameter represents the zero-based index of the element. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Projects each element of into a new form. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSMutableSet source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Projects each element of into a new form by incorporating each element's index. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element; the second parameter represents the zero-based index of the element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSMutableSet source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Sorts the elements of in ascending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderBy (this Foundation.NSMutableSet source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderBy ((IEnumerable) source, keySelector); + } + + /// Sorts the elements of in descending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted in descending order according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderByDescending (this Foundation.NSMutableSet source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderByDescending ((IEnumerable) source, keySelector); + } + + /// Bypasses a specified number of elements in and returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// The number of elements to skip before returning the remaining elements. + /// An that contains elements after the skipped ones. + /// Thrown when is . + public static IEnumerable Skip (this Foundation.NSMutableSet source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Skip ((IEnumerable) source, count); + } + + /// Bypasses elements in as long as a condition is true, then returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains the elements starting at the first element that does not satisfy the condition. + /// Thrown when or is . + public static IEnumerable SkipWhile (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SkipWhile ((IEnumerable) source, predicate); + } + + /// Returns a specified number of contiguous elements from the start of . + /// The element type of the collection. + /// The source collection. + /// The number of elements to return. + /// An that contains the specified number of elements from the start of . + /// Thrown when is . + public static IEnumerable Take (this Foundation.NSMutableSet source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Take ((IEnumerable) source, count); + } + + /// Returns elements from as long as a specified condition is true. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from as long as the condition is true. + /// Thrown when or is . + public static IEnumerable TakeWhile (this Foundation.NSMutableSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.TakeWhile ((IEnumerable) source, predicate); + } + + /// Returns distinct elements from . + /// The element type of the collection. + /// The source collection. + /// An that contains distinct elements from . + /// Thrown when is . + public static IEnumerable Distinct (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Distinct ((IEnumerable) source); + } + + /// Inverts the order of the elements in . + /// The element type of the collection. + /// The source collection. + /// An whose elements correspond to those of in reverse order. + /// Thrown when is . + public static IEnumerable Reverse (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Reverse ((IEnumerable) source); + } + + /// Concatenates with another sequence. + /// The element type of the collection. + /// The source collection. + /// The sequence to concatenate to . + /// An that contains the concatenated elements of the two sequences. + /// Thrown when or is . + public static IEnumerable Concat (this Foundation.NSMutableSet source, IEnumerable second) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (second); + return Enumerable.Concat ((IEnumerable) source, second); + } + + /// Creates a from . + /// The element type of the collection. + /// The source collection. + /// A that contains elements from . + /// Thrown when is . + public static List ToList (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToList ((IEnumerable) source); + } + + /// Creates an array from . + /// The element type of the collection. + /// The source collection. + /// An array that contains elements from . + /// Thrown when is . + public static TKey [] ToArray (this Foundation.NSMutableSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToArray ((IEnumerable) source); + } + + /// Applies an accumulator function over . + /// The element type of the collection. + /// The source collection. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + /// Thrown when is empty. + public static TKey Aggregate (this Foundation.NSMutableSet source, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, func); + } + + /// Applies an accumulator function over with a seed value. + /// The element type of the collection. + /// The type of the accumulator value. + /// The source collection. + /// The initial accumulator value. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + public static TAccumulate Aggregate (this Foundation.NSMutableSet source, TAccumulate seed, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, seed, func); + } + + // NSArray + /// Returns the first element of . + /// The element type of the collection. + /// The source collection. + /// The first element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey First (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.First ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey First (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.First ((IEnumerable) source, predicate); + } + + /// Returns the first element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The first element in , or if the collection is empty. + /// Thrown when is . + public static TKey? FirstOrDefault (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.FirstOrDefault ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? FirstOrDefault (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.FirstOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the last element of . + /// The element type of the collection. + /// The source collection. + /// The last element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey Last (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Last ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey Last (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Last ((IEnumerable) source, predicate); + } + + /// Returns the last element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The last element in , or if the collection is empty. + /// Thrown when is . + public static TKey? LastOrDefault (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LastOrDefault ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? LastOrDefault (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LastOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the only element of . + /// The element type of the collection. + /// The source collection. + /// The single element of . + /// Thrown when is . + /// Thrown when is empty or contains more than one element. + public static TKey Single (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Single ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element or more than one element satisfies the condition in . + public static TKey Single (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Single ((IEnumerable) source, predicate); + } + + /// Returns the only element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The single element of , or if the collection is empty. + /// Thrown when is . + /// Thrown when contains more than one element. + public static TKey? SingleOrDefault (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.SingleOrDefault ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in , or if no such element is found. + /// Thrown when or is . + /// Thrown when more than one element satisfies the condition in . + public static TKey? SingleOrDefault (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SingleOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the element at a specified index in . + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in . + /// Thrown when is . + /// Thrown when is less than 0 or greater than or equal to the number of elements in . + public static TKey ElementAt (this Foundation.NSArray source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAt ((IEnumerable) source, index); + } + + /// Returns the element at a specified index in , or if the index is out of range. + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in , or if the index is out of range. + /// Thrown when is . + public static TKey? ElementAtOrDefault (this Foundation.NSArray source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAtOrDefault ((IEnumerable) source, index); + } + + /// Determines whether contains any elements. + /// The element type of the collection. + /// The source collection. + /// if contains any elements; otherwise . + /// Thrown when is . + public static bool Any (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Any ((IEnumerable) source); + } + + /// Determines whether any element of satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if any element in passes the test in ; otherwise . + /// Thrown when or is . + public static bool Any (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Any ((IEnumerable) source, predicate); + } + + /// Determines whether all elements of satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if every element in passes the test in , or if the collection is empty; otherwise . + /// Thrown when or is . + public static bool All (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.All ((IEnumerable) source, predicate); + } + + /// Returns the number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static int Count (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Count ((IEnumerable) source); + } + + /// Returns the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static int Count (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Count ((IEnumerable) source, predicate); + } + + /// Returns a that represents the total number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static long LongCount (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LongCount ((IEnumerable) source); + } + + /// Returns a that represents the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static long LongCount (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LongCount ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate that incorporates each element's index. + /// The element type of the collection. + /// The source collection. + /// A function to test each element; the second parameter represents the zero-based index of the element. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Projects each element of into a new form. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSArray source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Projects each element of into a new form by incorporating each element's index. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element; the second parameter represents the zero-based index of the element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSArray source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Sorts the elements of in ascending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderBy (this Foundation.NSArray source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderBy ((IEnumerable) source, keySelector); + } + + /// Sorts the elements of in descending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted in descending order according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderByDescending (this Foundation.NSArray source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderByDescending ((IEnumerable) source, keySelector); + } + + /// Bypasses a specified number of elements in and returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// The number of elements to skip before returning the remaining elements. + /// An that contains elements after the skipped ones. + /// Thrown when is . + public static IEnumerable Skip (this Foundation.NSArray source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Skip ((IEnumerable) source, count); + } + + /// Bypasses elements in as long as a condition is true, then returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains the elements starting at the first element that does not satisfy the condition. + /// Thrown when or is . + public static IEnumerable SkipWhile (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SkipWhile ((IEnumerable) source, predicate); + } + + /// Returns a specified number of contiguous elements from the start of . + /// The element type of the collection. + /// The source collection. + /// The number of elements to return. + /// An that contains the specified number of elements from the start of . + /// Thrown when is . + public static IEnumerable Take (this Foundation.NSArray source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Take ((IEnumerable) source, count); + } + + /// Returns elements from as long as a specified condition is true. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from as long as the condition is true. + /// Thrown when or is . + public static IEnumerable TakeWhile (this Foundation.NSArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.TakeWhile ((IEnumerable) source, predicate); + } + + /// Returns distinct elements from . + /// The element type of the collection. + /// The source collection. + /// An that contains distinct elements from . + /// Thrown when is . + public static IEnumerable Distinct (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Distinct ((IEnumerable) source); + } + + /// Inverts the order of the elements in . + /// The element type of the collection. + /// The source collection. + /// An whose elements correspond to those of in reverse order. + /// Thrown when is . + public static IEnumerable Reverse (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Reverse ((IEnumerable) source); + } + + /// Concatenates with another sequence. + /// The element type of the collection. + /// The source collection. + /// The sequence to concatenate to . + /// An that contains the concatenated elements of the two sequences. + /// Thrown when or is . + public static IEnumerable Concat (this Foundation.NSArray source, IEnumerable second) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (second); + return Enumerable.Concat ((IEnumerable) source, second); + } + + /// Creates a from . + /// The element type of the collection. + /// The source collection. + /// A that contains elements from . + /// Thrown when is . + public static List ToList (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToList ((IEnumerable) source); + } + + /// Creates an array from . + /// The element type of the collection. + /// The source collection. + /// An array that contains elements from . + /// Thrown when is . + public static TKey [] ToArray (this Foundation.NSArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToArray ((IEnumerable) source); + } + + /// Applies an accumulator function over . + /// The element type of the collection. + /// The source collection. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + /// Thrown when is empty. + public static TKey Aggregate (this Foundation.NSArray source, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, func); + } + + /// Applies an accumulator function over with a seed value. + /// The element type of the collection. + /// The type of the accumulator value. + /// The source collection. + /// The initial accumulator value. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + public static TAccumulate Aggregate (this Foundation.NSArray source, TAccumulate seed, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, seed, func); + } + + // NSMutableArray + /// Returns the first element of . + /// The element type of the collection. + /// The source collection. + /// The first element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey First (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.First ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey First (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.First ((IEnumerable) source, predicate); + } + + /// Returns the first element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The first element in , or if the collection is empty. + /// Thrown when is . + public static TKey? FirstOrDefault (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.FirstOrDefault ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? FirstOrDefault (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.FirstOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the last element of . + /// The element type of the collection. + /// The source collection. + /// The last element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey Last (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Last ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey Last (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Last ((IEnumerable) source, predicate); + } + + /// Returns the last element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The last element in , or if the collection is empty. + /// Thrown when is . + public static TKey? LastOrDefault (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LastOrDefault ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? LastOrDefault (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LastOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the only element of . + /// The element type of the collection. + /// The source collection. + /// The single element of . + /// Thrown when is . + /// Thrown when is empty or contains more than one element. + public static TKey Single (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Single ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element or more than one element satisfies the condition in . + public static TKey Single (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Single ((IEnumerable) source, predicate); + } + + /// Returns the only element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The single element of , or if the collection is empty. + /// Thrown when is . + /// Thrown when contains more than one element. + public static TKey? SingleOrDefault (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.SingleOrDefault ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in , or if no such element is found. + /// Thrown when or is . + /// Thrown when more than one element satisfies the condition in . + public static TKey? SingleOrDefault (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SingleOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the element at a specified index in . + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in . + /// Thrown when is . + /// Thrown when is less than 0 or greater than or equal to the number of elements in . + public static TKey ElementAt (this Foundation.NSMutableArray source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAt ((IEnumerable) source, index); + } + + /// Returns the element at a specified index in , or if the index is out of range. + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in , or if the index is out of range. + /// Thrown when is . + public static TKey? ElementAtOrDefault (this Foundation.NSMutableArray source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAtOrDefault ((IEnumerable) source, index); + } + + /// Determines whether contains any elements. + /// The element type of the collection. + /// The source collection. + /// if contains any elements; otherwise . + /// Thrown when is . + public static bool Any (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Any ((IEnumerable) source); + } + + /// Determines whether any element of satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if any element in passes the test in ; otherwise . + /// Thrown when or is . + public static bool Any (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Any ((IEnumerable) source, predicate); + } + + /// Determines whether all elements of satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if every element in passes the test in , or if the collection is empty; otherwise . + /// Thrown when or is . + public static bool All (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.All ((IEnumerable) source, predicate); + } + + /// Returns the number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static int Count (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Count ((IEnumerable) source); + } + + /// Returns the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static int Count (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Count ((IEnumerable) source, predicate); + } + + /// Returns a that represents the total number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static long LongCount (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LongCount ((IEnumerable) source); + } + + /// Returns a that represents the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static long LongCount (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LongCount ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate that incorporates each element's index. + /// The element type of the collection. + /// The source collection. + /// A function to test each element; the second parameter represents the zero-based index of the element. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Projects each element of into a new form. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSMutableArray source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Projects each element of into a new form by incorporating each element's index. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element; the second parameter represents the zero-based index of the element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSMutableArray source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Sorts the elements of in ascending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderBy (this Foundation.NSMutableArray source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderBy ((IEnumerable) source, keySelector); + } + + /// Sorts the elements of in descending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted in descending order according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderByDescending (this Foundation.NSMutableArray source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderByDescending ((IEnumerable) source, keySelector); + } + + /// Bypasses a specified number of elements in and returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// The number of elements to skip before returning the remaining elements. + /// An that contains elements after the skipped ones. + /// Thrown when is . + public static IEnumerable Skip (this Foundation.NSMutableArray source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Skip ((IEnumerable) source, count); + } + + /// Bypasses elements in as long as a condition is true, then returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains the elements starting at the first element that does not satisfy the condition. + /// Thrown when or is . + public static IEnumerable SkipWhile (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SkipWhile ((IEnumerable) source, predicate); + } + + /// Returns a specified number of contiguous elements from the start of . + /// The element type of the collection. + /// The source collection. + /// The number of elements to return. + /// An that contains the specified number of elements from the start of . + /// Thrown when is . + public static IEnumerable Take (this Foundation.NSMutableArray source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Take ((IEnumerable) source, count); + } + + /// Returns elements from as long as a specified condition is true. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from as long as the condition is true. + /// Thrown when or is . + public static IEnumerable TakeWhile (this Foundation.NSMutableArray source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.TakeWhile ((IEnumerable) source, predicate); + } + + /// Returns distinct elements from . + /// The element type of the collection. + /// The source collection. + /// An that contains distinct elements from . + /// Thrown when is . + public static IEnumerable Distinct (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Distinct ((IEnumerable) source); + } + + /// Inverts the order of the elements in . + /// The element type of the collection. + /// The source collection. + /// An whose elements correspond to those of in reverse order. + /// Thrown when is . + public static IEnumerable Reverse (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Reverse ((IEnumerable) source); + } + + /// Concatenates with another sequence. + /// The element type of the collection. + /// The source collection. + /// The sequence to concatenate to . + /// An that contains the concatenated elements of the two sequences. + /// Thrown when or is . + public static IEnumerable Concat (this Foundation.NSMutableArray source, IEnumerable second) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (second); + return Enumerable.Concat ((IEnumerable) source, second); + } + + /// Creates a from . + /// The element type of the collection. + /// The source collection. + /// A that contains elements from . + /// Thrown when is . + public static List ToList (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToList ((IEnumerable) source); + } + + /// Creates an array from . + /// The element type of the collection. + /// The source collection. + /// An array that contains elements from . + /// Thrown when is . + public static TKey [] ToArray (this Foundation.NSMutableArray source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToArray ((IEnumerable) source); + } + + /// Applies an accumulator function over . + /// The element type of the collection. + /// The source collection. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + /// Thrown when is empty. + public static TKey Aggregate (this Foundation.NSMutableArray source, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, func); + } + + /// Applies an accumulator function over with a seed value. + /// The element type of the collection. + /// The type of the accumulator value. + /// The source collection. + /// The initial accumulator value. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + public static TAccumulate Aggregate (this Foundation.NSMutableArray source, TAccumulate seed, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, seed, func); + } + + // NSOrderedSet + /// Returns the first element of . + /// The element type of the collection. + /// The source collection. + /// The first element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey First (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.First ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey First (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.First ((IEnumerable) source, predicate); + } + + /// Returns the first element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The first element in , or if the collection is empty. + /// Thrown when is . + public static TKey? FirstOrDefault (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.FirstOrDefault ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? FirstOrDefault (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.FirstOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the last element of . + /// The element type of the collection. + /// The source collection. + /// The last element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey Last (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Last ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey Last (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Last ((IEnumerable) source, predicate); + } + + /// Returns the last element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The last element in , or if the collection is empty. + /// Thrown when is . + public static TKey? LastOrDefault (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LastOrDefault ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? LastOrDefault (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LastOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the only element of . + /// The element type of the collection. + /// The source collection. + /// The single element of . + /// Thrown when is . + /// Thrown when is empty or contains more than one element. + public static TKey Single (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Single ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element or more than one element satisfies the condition in . + public static TKey Single (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Single ((IEnumerable) source, predicate); + } + + /// Returns the only element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The single element of , or if the collection is empty. + /// Thrown when is . + /// Thrown when contains more than one element. + public static TKey? SingleOrDefault (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.SingleOrDefault ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in , or if no such element is found. + /// Thrown when or is . + /// Thrown when more than one element satisfies the condition in . + public static TKey? SingleOrDefault (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SingleOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the element at a specified index in . + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in . + /// Thrown when is . + /// Thrown when is less than 0 or greater than or equal to the number of elements in . + public static TKey ElementAt (this Foundation.NSOrderedSet source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAt ((IEnumerable) source, index); + } + + /// Returns the element at a specified index in , or if the index is out of range. + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in , or if the index is out of range. + /// Thrown when is . + public static TKey? ElementAtOrDefault (this Foundation.NSOrderedSet source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAtOrDefault ((IEnumerable) source, index); + } + + /// Determines whether contains any elements. + /// The element type of the collection. + /// The source collection. + /// if contains any elements; otherwise . + /// Thrown when is . + public static bool Any (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Any ((IEnumerable) source); + } + + /// Determines whether any element of satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if any element in passes the test in ; otherwise . + /// Thrown when or is . + public static bool Any (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Any ((IEnumerable) source, predicate); + } + + /// Determines whether all elements of satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if every element in passes the test in , or if the collection is empty; otherwise . + /// Thrown when or is . + public static bool All (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.All ((IEnumerable) source, predicate); + } + + /// Returns the number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static int Count (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Count ((IEnumerable) source); + } + + /// Returns the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static int Count (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Count ((IEnumerable) source, predicate); + } + + /// Returns a that represents the total number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static long LongCount (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LongCount ((IEnumerable) source); + } + + /// Returns a that represents the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static long LongCount (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LongCount ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate that incorporates each element's index. + /// The element type of the collection. + /// The source collection. + /// A function to test each element; the second parameter represents the zero-based index of the element. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Projects each element of into a new form. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSOrderedSet source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Projects each element of into a new form by incorporating each element's index. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element; the second parameter represents the zero-based index of the element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSOrderedSet source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Sorts the elements of in ascending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderBy (this Foundation.NSOrderedSet source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderBy ((IEnumerable) source, keySelector); + } + + /// Sorts the elements of in descending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted in descending order according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderByDescending (this Foundation.NSOrderedSet source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderByDescending ((IEnumerable) source, keySelector); + } + + /// Bypasses a specified number of elements in and returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// The number of elements to skip before returning the remaining elements. + /// An that contains elements after the skipped ones. + /// Thrown when is . + public static IEnumerable Skip (this Foundation.NSOrderedSet source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Skip ((IEnumerable) source, count); + } + + /// Bypasses elements in as long as a condition is true, then returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains the elements starting at the first element that does not satisfy the condition. + /// Thrown when or is . + public static IEnumerable SkipWhile (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SkipWhile ((IEnumerable) source, predicate); + } + + /// Returns a specified number of contiguous elements from the start of . + /// The element type of the collection. + /// The source collection. + /// The number of elements to return. + /// An that contains the specified number of elements from the start of . + /// Thrown when is . + public static IEnumerable Take (this Foundation.NSOrderedSet source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Take ((IEnumerable) source, count); + } + + /// Returns elements from as long as a specified condition is true. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from as long as the condition is true. + /// Thrown when or is . + public static IEnumerable TakeWhile (this Foundation.NSOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.TakeWhile ((IEnumerable) source, predicate); + } + + /// Returns distinct elements from . + /// The element type of the collection. + /// The source collection. + /// An that contains distinct elements from . + /// Thrown when is . + public static IEnumerable Distinct (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Distinct ((IEnumerable) source); + } + + /// Inverts the order of the elements in . + /// The element type of the collection. + /// The source collection. + /// An whose elements correspond to those of in reverse order. + /// Thrown when is . + public static IEnumerable Reverse (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Reverse ((IEnumerable) source); + } + + /// Concatenates with another sequence. + /// The element type of the collection. + /// The source collection. + /// The sequence to concatenate to . + /// An that contains the concatenated elements of the two sequences. + /// Thrown when or is . + public static IEnumerable Concat (this Foundation.NSOrderedSet source, IEnumerable second) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (second); + return Enumerable.Concat ((IEnumerable) source, second); + } + + /// Creates a from . + /// The element type of the collection. + /// The source collection. + /// A that contains elements from . + /// Thrown when is . + public static List ToList (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToList ((IEnumerable) source); + } + + /// Creates an array from . + /// The element type of the collection. + /// The source collection. + /// An array that contains elements from . + /// Thrown when is . + public static TKey [] ToArray (this Foundation.NSOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToArray ((IEnumerable) source); + } + + /// Applies an accumulator function over . + /// The element type of the collection. + /// The source collection. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + /// Thrown when is empty. + public static TKey Aggregate (this Foundation.NSOrderedSet source, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, func); + } + + /// Applies an accumulator function over with a seed value. + /// The element type of the collection. + /// The type of the accumulator value. + /// The source collection. + /// The initial accumulator value. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + public static TAccumulate Aggregate (this Foundation.NSOrderedSet source, TAccumulate seed, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, seed, func); + } + + // NSMutableOrderedSet + /// Returns the first element of . + /// The element type of the collection. + /// The source collection. + /// The first element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey First (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.First ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey First (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.First ((IEnumerable) source, predicate); + } + + /// Returns the first element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The first element in , or if the collection is empty. + /// Thrown when is . + public static TKey? FirstOrDefault (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.FirstOrDefault ((IEnumerable) source); + } + + /// Returns the first element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The first element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? FirstOrDefault (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.FirstOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the last element of . + /// The element type of the collection. + /// The source collection. + /// The last element in . + /// Thrown when is . + /// Thrown when is empty. + public static TKey Last (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Last ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element satisfies the condition in . + public static TKey Last (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Last ((IEnumerable) source, predicate); + } + + /// Returns the last element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The last element in , or if the collection is empty. + /// Thrown when is . + public static TKey? LastOrDefault (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LastOrDefault ((IEnumerable) source); + } + + /// Returns the last element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The last element in that passes the test in , or if no such element is found. + /// Thrown when or is . + public static TKey? LastOrDefault (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LastOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the only element of . + /// The element type of the collection. + /// The source collection. + /// The single element of . + /// Thrown when is . + /// Thrown when is empty or contains more than one element. + public static TKey Single (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Single ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in . + /// Thrown when or is . + /// Thrown when no element or more than one element satisfies the condition in . + public static TKey Single (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Single ((IEnumerable) source, predicate); + } + + /// Returns the only element of , or if the collection is empty. + /// The element type of the collection. + /// The source collection. + /// The single element of , or if the collection is empty. + /// Thrown when is . + /// Thrown when contains more than one element. + public static TKey? SingleOrDefault (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.SingleOrDefault ((IEnumerable) source); + } + + /// Returns the only element of that satisfies a condition, or if no such element is found. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The single element in that passes the test in , or if no such element is found. + /// Thrown when or is . + /// Thrown when more than one element satisfies the condition in . + public static TKey? SingleOrDefault (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SingleOrDefault ((IEnumerable) source, predicate); + } + + /// Returns the element at a specified index in . + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in . + /// Thrown when is . + /// Thrown when is less than 0 or greater than or equal to the number of elements in . + public static TKey ElementAt (this Foundation.NSMutableOrderedSet source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAt ((IEnumerable) source, index); + } + + /// Returns the element at a specified index in , or if the index is out of range. + /// The element type of the collection. + /// The source collection. + /// The zero-based index of the element to retrieve. + /// The element at position in , or if the index is out of range. + /// Thrown when is . + public static TKey? ElementAtOrDefault (this Foundation.NSMutableOrderedSet source, int index) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ElementAtOrDefault ((IEnumerable) source, index); + } + + /// Determines whether contains any elements. + /// The element type of the collection. + /// The source collection. + /// if contains any elements; otherwise . + /// Thrown when is . + public static bool Any (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Any ((IEnumerable) source); + } + + /// Determines whether any element of satisfies a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if any element in passes the test in ; otherwise . + /// Thrown when or is . + public static bool Any (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Any ((IEnumerable) source, predicate); + } + + /// Determines whether all elements of satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// if every element in passes the test in , or if the collection is empty; otherwise . + /// Thrown when or is . + public static bool All (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.All ((IEnumerable) source, predicate); + } + + /// Returns the number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static int Count (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Count ((IEnumerable) source); + } + + /// Returns the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static int Count (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Count ((IEnumerable) source, predicate); + } + + /// Returns a that represents the total number of elements in . + /// The element type of the collection. + /// The source collection. + /// The number of elements in . + /// Thrown when is . + public static long LongCount (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.LongCount ((IEnumerable) source); + } + + /// Returns a that represents the number of elements in that satisfy a condition. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// The number of elements in that pass the test in . + /// Thrown when or is . + public static long LongCount (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.LongCount ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Filters elements of based on a predicate that incorporates each element's index. + /// The element type of the collection. + /// The source collection. + /// A function to test each element; the second parameter represents the zero-based index of the element. + /// An that contains elements from that satisfy the condition. + /// Thrown when or is . + public static IEnumerable Where (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.Where ((IEnumerable) source, predicate); + } + + /// Projects each element of into a new form. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSMutableOrderedSet source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Projects each element of into a new form by incorporating each element's index. + /// The element type of the collection. + /// The type of the value returned by . + /// The source collection. + /// A transform function to apply to each element; the second parameter represents the zero-based index of the element. + /// An whose elements are the result of invoking the transform function on each element of . + /// Thrown when or is . + public static IEnumerable Select (this Foundation.NSMutableOrderedSet source, Func selector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (selector); + return Enumerable.Select ((IEnumerable) source, selector); + } + + /// Sorts the elements of in ascending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderBy (this Foundation.NSMutableOrderedSet source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderBy ((IEnumerable) source, keySelector); + } + + /// Sorts the elements of in descending order according to a key. + /// The element type of the collection. + /// The type of the key returned by . + /// The source collection. + /// A function to extract a key from an element. + /// An whose elements are sorted in descending order according to a key. + /// Thrown when or is . + public static IOrderedEnumerable OrderByDescending (this Foundation.NSMutableOrderedSet source, Func keySelector) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (keySelector); + return Enumerable.OrderByDescending ((IEnumerable) source, keySelector); + } + + /// Bypasses a specified number of elements in and returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// The number of elements to skip before returning the remaining elements. + /// An that contains elements after the skipped ones. + /// Thrown when is . + public static IEnumerable Skip (this Foundation.NSMutableOrderedSet source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Skip ((IEnumerable) source, count); + } + + /// Bypasses elements in as long as a condition is true, then returns the remaining elements. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains the elements starting at the first element that does not satisfy the condition. + /// Thrown when or is . + public static IEnumerable SkipWhile (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.SkipWhile ((IEnumerable) source, predicate); + } + + /// Returns a specified number of contiguous elements from the start of . + /// The element type of the collection. + /// The source collection. + /// The number of elements to return. + /// An that contains the specified number of elements from the start of . + /// Thrown when is . + public static IEnumerable Take (this Foundation.NSMutableOrderedSet source, int count) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Take ((IEnumerable) source, count); + } + + /// Returns elements from as long as a specified condition is true. + /// The element type of the collection. + /// The source collection. + /// A function to test each element for a condition. + /// An that contains elements from as long as the condition is true. + /// Thrown when or is . + public static IEnumerable TakeWhile (this Foundation.NSMutableOrderedSet source, Func predicate) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (predicate); + return Enumerable.TakeWhile ((IEnumerable) source, predicate); + } + + /// Returns distinct elements from . + /// The element type of the collection. + /// The source collection. + /// An that contains distinct elements from . + /// Thrown when is . + public static IEnumerable Distinct (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Distinct ((IEnumerable) source); + } + + /// Inverts the order of the elements in . + /// The element type of the collection. + /// The source collection. + /// An whose elements correspond to those of in reverse order. + /// Thrown when is . + public static IEnumerable Reverse (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.Reverse ((IEnumerable) source); + } + + /// Concatenates with another sequence. + /// The element type of the collection. + /// The source collection. + /// The sequence to concatenate to . + /// An that contains the concatenated elements of the two sequences. + /// Thrown when or is . + public static IEnumerable Concat (this Foundation.NSMutableOrderedSet source, IEnumerable second) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (second); + return Enumerable.Concat ((IEnumerable) source, second); + } + + /// Creates a from . + /// The element type of the collection. + /// The source collection. + /// A that contains elements from . + /// Thrown when is . + public static List ToList (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToList ((IEnumerable) source); + } + + /// Creates an array from . + /// The element type of the collection. + /// The source collection. + /// An array that contains elements from . + /// Thrown when is . + public static TKey [] ToArray (this Foundation.NSMutableOrderedSet source) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + return Enumerable.ToArray ((IEnumerable) source); + } + + /// Applies an accumulator function over . + /// The element type of the collection. + /// The source collection. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + /// Thrown when is empty. + public static TKey Aggregate (this Foundation.NSMutableOrderedSet source, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, func); + } + + /// Applies an accumulator function over with a seed value. + /// The element type of the collection. + /// The type of the accumulator value. + /// The source collection. + /// The initial accumulator value. + /// An accumulator function to be invoked on each element. + /// The final accumulator value. + /// Thrown when or is . + public static TAccumulate Aggregate (this Foundation.NSMutableOrderedSet source, TAccumulate seed, Func func) where TKey : class, ObjCRuntime.INativeObject + { + ArgumentNullException.ThrowIfNull (source); + ArgumentNullException.ThrowIfNull (func); + return Enumerable.Aggregate ((IEnumerable) source, seed, func); + } + } +} diff --git a/src/frameworks.sources b/src/frameworks.sources index accf45f75e36..4ea1cd028503 100644 --- a/src/frameworks.sources +++ b/src/frameworks.sources @@ -852,6 +852,7 @@ FOUNDATION_SOURCES = \ Foundation/NSOrderedCollectionDifference.cs \ Foundation/NSOrderedCollectionDifference_1.cs \ Foundation/NSConnection.cs \ + Foundation/NSCollectionLinqExtensions.cs \ Foundation/NSData.cs \ Foundation/NSDate.cs \ Foundation/NSDictionary.cs \ diff --git a/tests/monotouch-test/Foundation/ArrayTest.cs b/tests/monotouch-test/Foundation/ArrayTest.cs index 8965e909538c..10cc12004972 100644 --- a/tests/monotouch-test/Foundation/ArrayTest.cs +++ b/tests/monotouch-test/Foundation/ArrayTest.cs @@ -199,5 +199,14 @@ public void Enumerator () Assert.That (list [0].ToString (), Is.EqualTo ("abc"), "Value"); } } + + [Test] + public void LinqFirstOrDefault_MutableArray () + { + using var array = new NSMutableArray (); + array.Add ((NSString) "abc"); + var first = array.FirstOrDefault (); + Assert.That (first, Is.EqualTo ((NSString) "abc"), "FirstOrDefault"); + } } } diff --git a/tests/monotouch-test/Foundation/NSCollectionLinqTest.cs b/tests/monotouch-test/Foundation/NSCollectionLinqTest.cs new file mode 100644 index 000000000000..03ab33120969 --- /dev/null +++ b/tests/monotouch-test/Foundation/NSCollectionLinqTest.cs @@ -0,0 +1,1521 @@ +using System.Collections.Generic; +using System.Linq; + +using Foundation; + +using NUnit.Framework; + +namespace MonoTouchFixtures.Foundation { + + [TestFixture] + [Preserve (AllMembers = true)] + public class NSCollectionLinqTest { + + // ----- helpers ----- + + static NSString S (string s) => (NSString) s; + + static NSSet MakeSet (params string [] items) + { + return new NSSet (items.Select (S).ToArray ()); + } + + static NSMutableSet MakeMutableSet (params string [] items) + { + return new NSMutableSet (items.Select (S).ToArray ()); + } + + static NSArray MakeArray (params string [] items) + { + return NSArray.FromNSObjects (items.Select (S).ToArray ()); + } + + static NSMutableArray MakeMutableArray (params string [] items) + { + var arr = new NSMutableArray (); + foreach (var s in items) + arr.Add (S (s)); + return arr; + } + + static NSOrderedSet MakeOrderedSet (params string [] items) + { + return new NSOrderedSet (items.Select (S).ToArray ()); + } + + static NSMutableOrderedSet MakeMutableOrderedSet (params string [] items) + { + var set = new NSMutableOrderedSet (); + foreach (var s in items) + set.Add (S (s)); + return set; + } + + // ===== NSSet ===== + + [Test] + public void NSSet_First () + { + using var set = MakeSet ("a"); + var first = set.First (); + Assert.That (first.ToString (), Is.EqualTo ("a"), "First"); + } + + [Test] + public void NSSet_First_Predicate () + { + using var set = MakeSet ("a", "b", "c"); + var found = set.First (s => s.ToString () == "b"); + Assert.That (found.ToString (), Is.EqualTo ("b"), "First predicate"); + } + + [Test] + public void NSSet_First_Empty_Throws () + { + using var set = MakeSet (); + Assert.Throws (() => set.First ()); + } + + [Test] + public void NSSet_FirstOrDefault_Empty () + { + using var set = MakeSet (); + Assert.That (set.FirstOrDefault (), Is.Null, "FirstOrDefault empty"); + } + + [Test] + public void NSSet_FirstOrDefault () + { + using var set = MakeSet ("x"); + Assert.That (set.FirstOrDefault (), Is.Not.Null, "FirstOrDefault"); + } + + [Test] + public void NSSet_FirstOrDefault_Predicate_NoMatch () + { + using var set = MakeSet ("a", "b"); + Assert.That (set.FirstOrDefault (s => s.ToString () == "z"), Is.Null, "no match"); + } + + [Test] + public void NSSet_Last () + { + using var set = MakeSet ("a"); + var last = set.Last (); + Assert.That (last.ToString (), Is.EqualTo ("a"), "Last"); + } + + [Test] + public void NSSet_Last_Predicate () + { + using var set = MakeSet ("a", "b", "c"); + var found = set.Last (s => s.ToString () != "c"); + Assert.That (found, Is.Not.Null, "Last predicate not null"); + } + + [Test] + public void NSSet_Last_Empty_Throws () + { + using var set = MakeSet (); + Assert.Throws (() => set.Last ()); + } + + [Test] + public void NSSet_LastOrDefault_Empty () + { + using var set = MakeSet (); + Assert.That (set.LastOrDefault (), Is.Null, "LastOrDefault empty"); + } + + [Test] + public void NSSet_LastOrDefault_Predicate_NoMatch () + { + using var set = MakeSet ("a"); + Assert.That (set.LastOrDefault (s => s.ToString () == "z"), Is.Null, "LastOrDefault no match"); + } + + [Test] + public void NSSet_Single () + { + using var set = MakeSet ("only"); + Assert.That (set.Single ().ToString (), Is.EqualTo ("only"), "Single"); + } + + [Test] + public void NSSet_Single_Throws_OnMultiple () + { + using var set = MakeSet ("a", "b"); + Assert.Throws (() => set.Single ()); + } + + [Test] + public void NSSet_SingleOrDefault_Empty () + { + using var set = MakeSet (); + Assert.That (set.SingleOrDefault (), Is.Null, "SingleOrDefault empty"); + } + + [Test] + public void NSSet_SingleOrDefault_Predicate () + { + using var set = MakeSet ("a", "b"); + Assert.That (set.SingleOrDefault (s => s.ToString () == "a")?.ToString (), Is.EqualTo ("a"), "SingleOrDefault predicate"); + } + + [Test] + public void NSSet_Any_Empty () + { + using var set = MakeSet (); + Assert.That (set.Any (), Is.False, "Any empty"); + } + + [Test] + public void NSSet_Any_NonEmpty () + { + using var set = MakeSet ("a"); + Assert.That (set.Any (), Is.True, "Any non-empty"); + } + + [Test] + public void NSSet_Any_Predicate () + { + using var set = MakeSet ("a", "b"); + Assert.That (set.Any (s => s.ToString () == "b"), Is.True, "Any predicate true"); + Assert.That (set.Any (s => s.ToString () == "z"), Is.False, "Any predicate false"); + } + + [Test] + public void NSSet_All () + { + using var set = MakeSet ("ab", "abc"); + Assert.That (set.All (s => s.Length > 1), Is.True, "All true"); + Assert.That (set.All (s => s.Length > 2), Is.False, "All false"); + } + + [Test] + public void NSSet_Count () + { + using var set = MakeSet ("a", "b", "c"); + Assert.That (set.Count (), Is.EqualTo (3), "Count"); + Assert.That (set.Count (s => s.ToString () == "a"), Is.EqualTo (1), "Count predicate"); + } + + [Test] + public void NSSet_LongCount () + { + using var set = MakeSet ("a", "b"); + Assert.That (set.LongCount (), Is.EqualTo (2L), "LongCount"); + Assert.That (set.LongCount (s => s.ToString () == "a"), Is.EqualTo (1L), "LongCount predicate"); + } + + [Test] + public void NSSet_Where () + { + using var set = MakeSet ("a", "bb", "ccc"); + var result = set.Where (s => s.Length > 1).ToList (); + Assert.That (result.Count, Is.EqualTo (2), "Where count"); + } + + [Test] + public void NSSet_Where_Index () + { + using var arr = MakeOrderedSet ("a", "b", "c"); + var result = arr.Where ((s, i) => i % 2 == 0).ToList (); + Assert.That (result.Count, Is.EqualTo (2), "Where index count"); + } + + [Test] + public void NSSet_Select () + { + using var set = MakeSet ("hello"); + var lengths = set.Select (s => s.Length).ToList (); + Assert.That (lengths, Contains.Item (5), "Select length"); + } + + [Test] + public void NSSet_Select_Index () + { + using var arr = MakeOrderedSet ("a", "b", "c"); + var indexed = arr.Select ((s, i) => $"{i}:{s}").ToList (); + Assert.That (indexed [0], Is.EqualTo ("0:a"), "Select index 0"); + Assert.That (indexed [1], Is.EqualTo ("1:b"), "Select index 1"); + } + + [Test] + public void NSSet_OrderBy () + { + using var set = MakeSet ("banana", "apple", "cherry"); + var sorted = set.OrderBy (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("apple"), "OrderBy first"); + Assert.That (sorted [2].ToString (), Is.EqualTo ("cherry"), "OrderBy last"); + } + + [Test] + public void NSSet_OrderByDescending () + { + using var set = MakeSet ("banana", "apple", "cherry"); + var sorted = set.OrderByDescending (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("cherry"), "OrderByDesc first"); + } + + [Test] + public void NSSet_Distinct () + { + using var set = MakeSet ("a", "b", "b"); + // NSSet already ensures uniqueness, but verify Distinct works + var distinct = set.Distinct ().ToList (); + Assert.That (distinct.Count, Is.EqualTo (2), "Distinct count"); + } + + [Test] + public void NSSet_Reverse () + { + using var arr = MakeOrderedSet ("a", "b", "c"); + var reversed = arr.Reverse ().ToList (); + Assert.That (reversed [0].ToString (), Is.EqualTo ("c"), "Reverse first"); + Assert.That (reversed [2].ToString (), Is.EqualTo ("a"), "Reverse last"); + } + + [Test] + public void NSSet_Concat () + { + using var set = MakeSet ("a", "b"); + var extra = new [] { S ("c"), S ("d") }; + var all = set.Concat (extra).ToList (); + Assert.That (all.Count, Is.EqualTo (4), "Concat count"); + } + + [Test] + public void NSSet_Skip_Take () + { + using var arr = MakeOrderedSet ("a", "b", "c", "d"); + var sliced = arr.Skip (1).Take (2).ToList (); + Assert.That (sliced.Count, Is.EqualTo (2), "Skip/Take count"); + Assert.That (sliced [0].ToString (), Is.EqualTo ("b"), "Skip/Take first"); + } + + [Test] + public void NSSet_SkipWhile_TakeWhile () + { + using var arr = MakeOrderedSet ("a", "b", "c"); + var skipped = arr.SkipWhile (s => s.ToString () == "a").ToList (); + Assert.That (skipped.Count, Is.EqualTo (2), "SkipWhile count"); + + var taken = arr.TakeWhile (s => s.ToString () != "c").ToList (); + Assert.That (taken.Count, Is.EqualTo (2), "TakeWhile count"); + } + + [Test] + public void NSSet_ElementAt () + { + using var arr = MakeOrderedSet ("a", "b", "c"); + Assert.That (arr.ElementAt (1).ToString (), Is.EqualTo ("b"), "ElementAt"); + } + + [Test] + public void NSSet_ElementAtOrDefault () + { + using var arr = MakeOrderedSet ("a", "b"); + Assert.That (arr.ElementAtOrDefault (0), Is.Not.Null, "ElementAtOrDefault in range"); + Assert.That (arr.ElementAtOrDefault (99), Is.Null, "ElementAtOrDefault out of range"); + } + + [Test] + public void NSSet_ToList () + { + using var set = MakeSet ("a", "b"); + var list = set.ToList (); + Assert.That (list, Is.TypeOf> (), "ToList type"); + Assert.That (list.Count, Is.EqualTo (2), "ToList count"); + } + + [Test] + public void NSSet_ToArray () + { + using var set = MakeSet ("a", "b"); + var arr = set.ToArray (); + Assert.That (arr, Is.TypeOf (), "ToArray type"); + Assert.That (arr.Length, Is.EqualTo (2), "ToArray length"); + } + + [Test] + public void NSSet_Aggregate () + { + using var arr = MakeOrderedSet ("a", "b", "c"); + var result = arr.Aggregate ((acc, s) => (NSString) (acc.ToString () + s.ToString ())); + Assert.That (result.ToString (), Is.EqualTo ("abc"), "Aggregate"); + } + + [Test] + public void NSSet_Aggregate_Seed () + { + using var arr = MakeOrderedSet ("a", "b", "c"); + var result = arr.Aggregate ("", (acc, s) => acc + s.ToString ()); + Assert.That (result, Is.EqualTo ("abc"), "Aggregate seed"); + } + + // ===== NSMutableSet ===== + + [Test] + public void NSMutableSet_First () + { + using var set = MakeMutableSet ("a"); + Assert.That (set.First ().ToString (), Is.EqualTo ("a"), "First"); + } + + [Test] + public void NSMutableSet_FirstOrDefault_Empty () + { + using var set = MakeMutableSet (); + Assert.That (set.FirstOrDefault (), Is.Null, "FirstOrDefault empty"); + } + + [Test] + public void NSMutableSet_Last () + { + using var set = MakeMutableSet ("a"); + Assert.That (set.Last ().ToString (), Is.EqualTo ("a"), "Last"); + } + + [Test] + public void NSMutableSet_LastOrDefault_Empty () + { + using var set = MakeMutableSet (); + Assert.That (set.LastOrDefault (), Is.Null, "LastOrDefault empty"); + } + + [Test] + public void NSMutableSet_Single () + { + using var set = MakeMutableSet ("only"); + Assert.That (set.Single ().ToString (), Is.EqualTo ("only"), "Single"); + } + + [Test] + public void NSMutableSet_SingleOrDefault_Empty () + { + using var set = MakeMutableSet (); + Assert.That (set.SingleOrDefault (), Is.Null, "SingleOrDefault empty"); + } + + [Test] + public void NSMutableSet_Any () + { + using var set = MakeMutableSet (); + Assert.That (set.Any (), Is.False, "Any empty"); + using var set2 = MakeMutableSet ("a"); + Assert.That (set2.Any (), Is.True, "Any non-empty"); + } + + [Test] + public void NSMutableSet_All () + { + using var set = MakeMutableSet ("ab", "abc"); + Assert.That (set.All (s => s.Length > 1), Is.True, "All true"); + } + + [Test] + public void NSMutableSet_Count () + { + using var set = MakeMutableSet ("a", "b", "c"); + Assert.That (set.Count (), Is.EqualTo (3), "Count"); + } + + [Test] + public void NSMutableSet_LongCount () + { + using var set = MakeMutableSet ("a", "b"); + Assert.That (set.LongCount (), Is.EqualTo (2L), "LongCount"); + } + + [Test] + public void NSMutableSet_Where () + { + using var set = MakeMutableSet ("a", "bb", "ccc"); + var result = set.Where (s => s.Length > 1).ToList (); + Assert.That (result.Count, Is.EqualTo (2), "Where count"); + } + + [Test] + public void NSMutableSet_Select () + { + using var set = MakeMutableSet ("hello"); + var lengths = set.Select (s => s.Length).ToList (); + Assert.That (lengths, Contains.Item (5), "Select length"); + } + + [Test] + public void NSMutableSet_OrderBy () + { + using var set = MakeMutableSet ("banana", "apple", "cherry"); + var sorted = set.OrderBy (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("apple"), "OrderBy first"); + } + + [Test] + public void NSMutableSet_Skip_Take () + { + using var arr = MakeOrderedSet ("a", "b", "c", "d"); + var sliced = arr.Skip (1).Take (2).ToList (); + Assert.That (sliced.Count, Is.EqualTo (2), "Skip/Take count"); + } + + [Test] + public void NSMutableSet_ToList () + { + using var set = MakeMutableSet ("a", "b"); + var list = set.ToList (); + Assert.That (list.Count, Is.EqualTo (2), "ToList count"); + } + + [Test] + public void NSMutableSet_ToArray () + { + using var set = MakeMutableSet ("a", "b"); + var arr = set.ToArray (); + Assert.That (arr.Length, Is.EqualTo (2), "ToArray length"); + } + + [Test] + public void NSMutableSet_Aggregate () + { + using var set = MakeMutableSet ("a"); + var result = set.Aggregate ((acc, s) => (NSString) (acc.ToString () + s.ToString ())); + Assert.That (result.ToString (), Is.EqualTo ("a"), "Aggregate"); + } + + // ===== NSArray ===== + + [Test] + public void NSArray_First () + { + using var arr = MakeArray ("a", "b", "c"); + Assert.That (arr.First ().ToString (), Is.EqualTo ("a"), "First"); + } + + [Test] + public void NSArray_First_Predicate () + { + using var arr = MakeArray ("a", "b", "c"); + Assert.That (arr.First (s => s.ToString () == "b").ToString (), Is.EqualTo ("b"), "First predicate"); + } + + [Test] + public void NSArray_First_Empty_Throws () + { + using var arr = MakeArray (); + Assert.Throws (() => arr.First ()); + } + + [Test] + public void NSArray_FirstOrDefault_Empty () + { + using var arr = MakeArray (); + Assert.That (arr.FirstOrDefault (), Is.Null, "FirstOrDefault empty"); + } + + [Test] + public void NSArray_FirstOrDefault () + { + using var arr = MakeArray ("x", "y"); + Assert.That (arr.FirstOrDefault ()?.ToString (), Is.EqualTo ("x"), "FirstOrDefault"); + } + + [Test] + public void NSArray_FirstOrDefault_Predicate () + { + using var arr = MakeArray ("a", "b", "c"); + Assert.That (arr.FirstOrDefault (s => s.ToString () == "b")?.ToString (), Is.EqualTo ("b"), "FirstOrDefault predicate match"); + Assert.That (arr.FirstOrDefault (s => s.ToString () == "z"), Is.Null, "FirstOrDefault predicate no match"); + } + + [Test] + public void NSArray_Last () + { + using var arr = MakeArray ("a", "b", "c"); + Assert.That (arr.Last ().ToString (), Is.EqualTo ("c"), "Last"); + } + + [Test] + public void NSArray_Last_Predicate () + { + using var arr = MakeArray ("a", "b", "c"); + Assert.That (arr.Last (s => s.ToString () != "c").ToString (), Is.EqualTo ("b"), "Last predicate"); + } + + [Test] + public void NSArray_Last_Empty_Throws () + { + using var arr = MakeArray (); + Assert.Throws (() => arr.Last ()); + } + + [Test] + public void NSArray_LastOrDefault_Empty () + { + using var arr = MakeArray (); + Assert.That (arr.LastOrDefault (), Is.Null, "LastOrDefault empty"); + } + + [Test] + public void NSArray_LastOrDefault_Predicate_NoMatch () + { + using var arr = MakeArray ("a", "b"); + Assert.That (arr.LastOrDefault (s => s.ToString () == "z"), Is.Null, "LastOrDefault no match"); + } + + [Test] + public void NSArray_Single () + { + using var arr = MakeArray ("only"); + Assert.That (arr.Single ().ToString (), Is.EqualTo ("only"), "Single"); + } + + [Test] + public void NSArray_Single_Throws_OnEmpty () + { + using var arr = MakeArray (); + Assert.Throws (() => arr.Single ()); + } + + [Test] + public void NSArray_Single_Throws_OnMultiple () + { + using var arr = MakeArray ("a", "b"); + Assert.Throws (() => arr.Single ()); + } + + [Test] + public void NSArray_SingleOrDefault_Empty () + { + using var arr = MakeArray (); + Assert.That (arr.SingleOrDefault (), Is.Null, "SingleOrDefault empty"); + } + + [Test] + public void NSArray_SingleOrDefault_Predicate () + { + using var arr = MakeArray ("a", "b"); + Assert.That (arr.SingleOrDefault (s => s.ToString () == "a")?.ToString (), Is.EqualTo ("a"), "SingleOrDefault predicate match"); + Assert.That (arr.SingleOrDefault (s => s.ToString () == "z"), Is.Null, "SingleOrDefault predicate no match"); + } + + [Test] + public void NSArray_ElementAt () + { + using var arr = MakeArray ("a", "b", "c"); + Assert.That (arr.ElementAt (0).ToString (), Is.EqualTo ("a"), "ElementAt 0"); + Assert.That (arr.ElementAt (2).ToString (), Is.EqualTo ("c"), "ElementAt 2"); + } + + [Test] + public void NSArray_ElementAt_OutOfRange_Throws () + { + using var arr = MakeArray ("a"); + Assert.Throws (() => arr.ElementAt (5)); + } + + [Test] + public void NSArray_ElementAtOrDefault () + { + using var arr = MakeArray ("a", "b"); + Assert.That (arr.ElementAtOrDefault (1)?.ToString (), Is.EqualTo ("b"), "ElementAtOrDefault in range"); + Assert.That (arr.ElementAtOrDefault (99), Is.Null, "ElementAtOrDefault out of range"); + } + + [Test] + public void NSArray_Any () + { + using var empty = MakeArray (); + Assert.That (empty.Any (), Is.False, "Any empty"); + using var arr = MakeArray ("a"); + Assert.That (arr.Any (), Is.True, "Any non-empty"); + Assert.That (arr.Any (s => s.ToString () == "a"), Is.True, "Any predicate true"); + Assert.That (arr.Any (s => s.ToString () == "z"), Is.False, "Any predicate false"); + } + + [Test] + public void NSArray_All () + { + using var arr = MakeArray ("ab", "abc"); + Assert.That (arr.All (s => s.Length > 1), Is.True, "All true"); + Assert.That (arr.All (s => s.Length > 2), Is.False, "All false"); + } + + [Test] + public void NSArray_Count () + { + using var arr = MakeArray ("a", "b", "c"); + Assert.That (arr.Count (), Is.EqualTo (3), "Count"); + Assert.That (arr.Count (s => s.ToString () == "a"), Is.EqualTo (1), "Count predicate"); + } + + [Test] + public void NSArray_LongCount () + { + using var arr = MakeArray ("a", "b"); + Assert.That (arr.LongCount (), Is.EqualTo (2L), "LongCount"); + Assert.That (arr.LongCount (s => s.ToString () == "a"), Is.EqualTo (1L), "LongCount predicate"); + } + + [Test] + public void NSArray_Where () + { + using var arr = MakeArray ("a", "bb", "ccc"); + var result = arr.Where (s => s.Length > 1).ToList (); + Assert.That (result.Count, Is.EqualTo (2), "Where count"); + } + + [Test] + public void NSArray_Where_Index () + { + using var arr = MakeArray ("a", "b", "c"); + var result = arr.Where ((s, i) => i % 2 == 0).ToList (); + Assert.That (result.Count, Is.EqualTo (2), "Where index count"); + Assert.That (result [0].ToString (), Is.EqualTo ("a"), "Where index [0]"); + Assert.That (result [1].ToString (), Is.EqualTo ("c"), "Where index [1]"); + } + + [Test] + public void NSArray_Select () + { + using var arr = MakeArray ("hello", "world"); + var lengths = arr.Select (s => s.Length).ToList (); + Assert.That (lengths [0], Is.EqualTo (5), "Select length [0]"); + Assert.That (lengths [1], Is.EqualTo (5), "Select length [1]"); + } + + [Test] + public void NSArray_Select_Index () + { + using var arr = MakeArray ("a", "b", "c"); + var indexed = arr.Select ((s, i) => $"{i}:{s}").ToList (); + Assert.That (indexed [0], Is.EqualTo ("0:a"), "Select index 0"); + Assert.That (indexed [2], Is.EqualTo ("2:c"), "Select index 2"); + } + + [Test] + public void NSArray_OrderBy () + { + using var arr = MakeArray ("banana", "apple", "cherry"); + var sorted = arr.OrderBy (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("apple"), "OrderBy first"); + Assert.That (sorted [1].ToString (), Is.EqualTo ("banana"), "OrderBy second"); + Assert.That (sorted [2].ToString (), Is.EqualTo ("cherry"), "OrderBy third"); + } + + [Test] + public void NSArray_OrderByDescending () + { + using var arr = MakeArray ("banana", "apple", "cherry"); + var sorted = arr.OrderByDescending (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("cherry"), "OrderByDesc first"); + Assert.That (sorted [2].ToString (), Is.EqualTo ("apple"), "OrderByDesc last"); + } + + [Test] + public void NSArray_Skip () + { + using var arr = MakeArray ("a", "b", "c", "d"); + var skipped = arr.Skip (2).ToList (); + Assert.That (skipped.Count, Is.EqualTo (2), "Skip count"); + Assert.That (skipped [0].ToString (), Is.EqualTo ("c"), "Skip [0]"); + } + + [Test] + public void NSArray_SkipWhile () + { + using var arr = MakeArray ("a", "b", "c"); + var result = arr.SkipWhile (s => s.ToString () != "b").ToList (); + Assert.That (result.Count, Is.EqualTo (2), "SkipWhile count"); + Assert.That (result [0].ToString (), Is.EqualTo ("b"), "SkipWhile first"); + } + + [Test] + public void NSArray_Take () + { + using var arr = MakeArray ("a", "b", "c"); + var taken = arr.Take (2).ToList (); + Assert.That (taken.Count, Is.EqualTo (2), "Take count"); + Assert.That (taken [1].ToString (), Is.EqualTo ("b"), "Take [1]"); + } + + [Test] + public void NSArray_TakeWhile () + { + using var arr = MakeArray ("a", "b", "c"); + var taken = arr.TakeWhile (s => s.ToString () != "c").ToList (); + Assert.That (taken.Count, Is.EqualTo (2), "TakeWhile count"); + } + + [Test] + public void NSArray_Distinct () + { + using var arr = MakeArray ("a", "a", "b"); + var distinct = arr.Distinct ().ToList (); + Assert.That (distinct.Count, Is.EqualTo (2), "Distinct count"); + } + + [Test] + public void NSArray_Reverse () + { + using var arr = MakeArray ("a", "b", "c"); + var reversed = arr.Reverse ().ToList (); + Assert.That (reversed [0].ToString (), Is.EqualTo ("c"), "Reverse [0]"); + Assert.That (reversed [2].ToString (), Is.EqualTo ("a"), "Reverse [2]"); + } + + [Test] + public void NSArray_Concat () + { + using var arr = MakeArray ("a", "b"); + var extra = new [] { S ("c") }; + var all = arr.Concat (extra).ToList (); + Assert.That (all.Count, Is.EqualTo (3), "Concat count"); + Assert.That (all [2].ToString (), Is.EqualTo ("c"), "Concat last"); + } + + [Test] + public void NSArray_ToList () + { + using var arr = MakeArray ("a", "b", "c"); + var list = arr.ToList (); + Assert.That (list, Is.TypeOf> (), "ToList type"); + Assert.That (list.Count, Is.EqualTo (3), "ToList count"); + } + + [Test] + public void NSArray_ToArray () + { + using var arr = MakeArray ("a", "b"); + var result = arr.ToArray (); + Assert.That (result, Is.TypeOf (), "ToArray type"); + Assert.That (result.Length, Is.EqualTo (2), "ToArray length"); + } + + [Test] + public void NSArray_Aggregate () + { + using var arr = MakeArray ("a", "b", "c"); + var result = arr.Aggregate ((acc, s) => (NSString) (acc.ToString () + s.ToString ())); + Assert.That (result.ToString (), Is.EqualTo ("abc"), "Aggregate"); + } + + [Test] + public void NSArray_Aggregate_Seed () + { + using var arr = MakeArray ("a", "b", "c"); + var result = arr.Aggregate ("", (acc, s) => acc + s.ToString ()); + Assert.That (result, Is.EqualTo ("abc"), "Aggregate seed"); + } + + // ===== NSMutableArray ===== + + [Test] + public void NSMutableArray_First () + { + using var arr = MakeMutableArray ("a", "b"); + Assert.That (arr.First ().ToString (), Is.EqualTo ("a"), "First"); + } + + [Test] + public void NSMutableArray_First_Empty_Throws () + { + using var arr = MakeMutableArray (); + Assert.Throws (() => arr.First ()); + } + + [Test] + public void NSMutableArray_FirstOrDefault_Empty () + { + using var arr = MakeMutableArray (); + Assert.That (arr.FirstOrDefault (), Is.Null, "FirstOrDefault empty"); + } + + [Test] + public void NSMutableArray_FirstOrDefault () + { + using var arr = MakeMutableArray ("x", "y"); + Assert.That (arr.FirstOrDefault ()?.ToString (), Is.EqualTo ("x"), "FirstOrDefault"); + } + + [Test] + public void NSMutableArray_Last () + { + using var arr = MakeMutableArray ("a", "b", "c"); + Assert.That (arr.Last ().ToString (), Is.EqualTo ("c"), "Last"); + } + + [Test] + public void NSMutableArray_LastOrDefault_Empty () + { + using var arr = MakeMutableArray (); + Assert.That (arr.LastOrDefault (), Is.Null, "LastOrDefault empty"); + } + + [Test] + public void NSMutableArray_Single () + { + using var arr = MakeMutableArray ("only"); + Assert.That (arr.Single ().ToString (), Is.EqualTo ("only"), "Single"); + } + + [Test] + public void NSMutableArray_Single_Throws_OnMultiple () + { + using var arr = MakeMutableArray ("a", "b"); + Assert.Throws (() => arr.Single ()); + } + + [Test] + public void NSMutableArray_SingleOrDefault_Empty () + { + using var arr = MakeMutableArray (); + Assert.That (arr.SingleOrDefault (), Is.Null, "SingleOrDefault empty"); + } + + [Test] + public void NSMutableArray_ElementAt () + { + using var arr = MakeMutableArray ("a", "b", "c"); + Assert.That (arr.ElementAt (1).ToString (), Is.EqualTo ("b"), "ElementAt 1"); + } + + [Test] + public void NSMutableArray_ElementAtOrDefault () + { + using var arr = MakeMutableArray ("a"); + Assert.That (arr.ElementAtOrDefault (0)?.ToString (), Is.EqualTo ("a"), "ElementAtOrDefault in range"); + Assert.That (arr.ElementAtOrDefault (10), Is.Null, "ElementAtOrDefault out of range"); + } + + [Test] + public void NSMutableArray_Any () + { + using var empty = MakeMutableArray (); + Assert.That (empty.Any (), Is.False, "Any empty"); + using var arr = MakeMutableArray ("a"); + Assert.That (arr.Any (), Is.True, "Any non-empty"); + } + + [Test] + public void NSMutableArray_All () + { + using var arr = MakeMutableArray ("ab", "abc"); + Assert.That (arr.All (s => s.Length > 1), Is.True, "All true"); + } + + [Test] + public void NSMutableArray_Count () + { + using var arr = MakeMutableArray ("a", "b", "c"); + Assert.That (arr.Count (), Is.EqualTo (3), "Count"); + } + + [Test] + public void NSMutableArray_LongCount () + { + using var arr = MakeMutableArray ("a", "b"); + Assert.That (arr.LongCount (), Is.EqualTo (2L), "LongCount"); + } + + [Test] + public void NSMutableArray_Where () + { + using var arr = MakeMutableArray ("a", "bb", "ccc"); + var result = arr.Where (s => s.Length > 1).ToList (); + Assert.That (result.Count, Is.EqualTo (2), "Where count"); + } + + [Test] + public void NSMutableArray_Select () + { + using var arr = MakeMutableArray ("hello", "world"); + var lengths = arr.Select (s => s.Length).ToList (); + Assert.That (lengths [0], Is.EqualTo (5), "Select [0]"); + } + + [Test] + public void NSMutableArray_OrderBy () + { + using var arr = MakeMutableArray ("banana", "apple", "cherry"); + var sorted = arr.OrderBy (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("apple"), "OrderBy first"); + } + + [Test] + public void NSMutableArray_OrderByDescending () + { + using var arr = MakeMutableArray ("banana", "apple", "cherry"); + var sorted = arr.OrderByDescending (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("cherry"), "OrderByDesc first"); + } + + [Test] + public void NSMutableArray_Skip_Take () + { + using var arr = MakeMutableArray ("a", "b", "c", "d"); + var sliced = arr.Skip (1).Take (2).ToList (); + Assert.That (sliced.Count, Is.EqualTo (2), "Skip/Take count"); + } + + [Test] + public void NSMutableArray_SkipWhile_TakeWhile () + { + using var arr = MakeMutableArray ("a", "b", "c"); + var skipped = arr.SkipWhile (s => s.ToString () == "a").ToList (); + Assert.That (skipped.Count, Is.EqualTo (2), "SkipWhile count"); + var taken = arr.TakeWhile (s => s.ToString () != "c").ToList (); + Assert.That (taken.Count, Is.EqualTo (2), "TakeWhile count"); + } + + [Test] + public void NSMutableArray_Distinct () + { + using var arr = MakeMutableArray ("a", "a", "b"); + var distinct = arr.Distinct ().ToList (); + Assert.That (distinct.Count, Is.EqualTo (2), "Distinct count"); + } + + [Test] + public void NSMutableArray_Reverse () + { + using var arr = MakeMutableArray ("a", "b", "c"); + var reversed = arr.Reverse ().ToList (); + Assert.That (reversed [0].ToString (), Is.EqualTo ("c"), "Reverse [0]"); + } + + [Test] + public void NSMutableArray_Concat () + { + using var arr = MakeMutableArray ("a"); + var all = arr.Concat (new [] { S ("b") }).ToList (); + Assert.That (all.Count, Is.EqualTo (2), "Concat count"); + } + + [Test] + public void NSMutableArray_ToList () + { + using var arr = MakeMutableArray ("a", "b"); + var list = arr.ToList (); + Assert.That (list.Count, Is.EqualTo (2), "ToList count"); + } + + [Test] + public void NSMutableArray_ToArray () + { + using var arr = MakeMutableArray ("a", "b"); + var result = arr.ToArray (); + Assert.That (result.Length, Is.EqualTo (2), "ToArray length"); + } + + [Test] + public void NSMutableArray_Aggregate () + { + using var arr = MakeMutableArray ("a", "b", "c"); + var result = arr.Aggregate ((acc, s) => (NSString) (acc.ToString () + s.ToString ())); + Assert.That (result.ToString (), Is.EqualTo ("abc"), "Aggregate"); + } + + [Test] + public void NSMutableArray_Aggregate_Seed () + { + using var arr = MakeMutableArray ("a", "b", "c"); + var result = arr.Aggregate (0, (acc, s) => acc + s.Length); + Assert.That (result, Is.EqualTo (3), "Aggregate seed"); + } + + // ===== NSOrderedSet ===== + + [Test] + public void NSOrderedSet_First () + { + using var set = MakeOrderedSet ("a", "b", "c"); + Assert.That (set.First ().ToString (), Is.EqualTo ("a"), "First"); + } + + [Test] + public void NSOrderedSet_First_Predicate () + { + using var set = MakeOrderedSet ("a", "b", "c"); + Assert.That (set.First (s => s.ToString () == "c").ToString (), Is.EqualTo ("c"), "First predicate"); + } + + [Test] + public void NSOrderedSet_First_Empty_Throws () + { + using var set = MakeOrderedSet (); + Assert.Throws (() => set.First ()); + } + + [Test] + public void NSOrderedSet_FirstOrDefault_Empty () + { + using var set = MakeOrderedSet (); + Assert.That (set.FirstOrDefault (), Is.Null, "FirstOrDefault empty"); + } + + [Test] + public void NSOrderedSet_FirstOrDefault () + { + using var set = MakeOrderedSet ("x", "y"); + Assert.That (set.FirstOrDefault ()?.ToString (), Is.EqualTo ("x"), "FirstOrDefault"); + } + + [Test] + public void NSOrderedSet_Last () + { + using var set = MakeOrderedSet ("a", "b", "c"); + Assert.That (set.Last ().ToString (), Is.EqualTo ("c"), "Last"); + } + + [Test] + public void NSOrderedSet_Last_Predicate () + { + using var set = MakeOrderedSet ("a", "b", "c"); + Assert.That (set.Last (s => s.ToString () != "c").ToString (), Is.EqualTo ("b"), "Last predicate"); + } + + [Test] + public void NSOrderedSet_Last_Empty_Throws () + { + using var set = MakeOrderedSet (); + Assert.Throws (() => set.Last ()); + } + + [Test] + public void NSOrderedSet_LastOrDefault_Empty () + { + using var set = MakeOrderedSet (); + Assert.That (set.LastOrDefault (), Is.Null, "LastOrDefault empty"); + } + + [Test] + public void NSOrderedSet_Single () + { + using var set = MakeOrderedSet ("only"); + Assert.That (set.Single ().ToString (), Is.EqualTo ("only"), "Single"); + } + + [Test] + public void NSOrderedSet_Single_Throws_OnMultiple () + { + using var set = MakeOrderedSet ("a", "b"); + Assert.Throws (() => set.Single ()); + } + + [Test] + public void NSOrderedSet_SingleOrDefault_Empty () + { + using var set = MakeOrderedSet (); + Assert.That (set.SingleOrDefault (), Is.Null, "SingleOrDefault empty"); + } + + [Test] + public void NSOrderedSet_SingleOrDefault_Predicate () + { + using var set = MakeOrderedSet ("a", "b"); + Assert.That (set.SingleOrDefault (s => s.ToString () == "a")?.ToString (), Is.EqualTo ("a"), "SingleOrDefault predicate match"); + } + + [Test] + public void NSOrderedSet_ElementAt () + { + using var set = MakeOrderedSet ("a", "b", "c"); + Assert.That (set.ElementAt (1).ToString (), Is.EqualTo ("b"), "ElementAt 1"); + } + + [Test] + public void NSOrderedSet_ElementAtOrDefault () + { + using var set = MakeOrderedSet ("a"); + Assert.That (set.ElementAtOrDefault (0)?.ToString (), Is.EqualTo ("a"), "in range"); + Assert.That (set.ElementAtOrDefault (5), Is.Null, "out of range"); + } + + [Test] + public void NSOrderedSet_Any () + { + using var empty = MakeOrderedSet (); + Assert.That (empty.Any (), Is.False, "Any empty"); + using var set = MakeOrderedSet ("a"); + Assert.That (set.Any (), Is.True, "Any non-empty"); + Assert.That (set.Any (s => s.ToString () == "a"), Is.True, "Any predicate true"); + Assert.That (set.Any (s => s.ToString () == "z"), Is.False, "Any predicate false"); + } + + [Test] + public void NSOrderedSet_All () + { + using var set = MakeOrderedSet ("ab", "abc"); + Assert.That (set.All (s => s.Length > 1), Is.True, "All true"); + Assert.That (set.All (s => s.Length > 2), Is.False, "All false"); + } + + [Test] + public void NSOrderedSet_Count () + { + using var set = MakeOrderedSet ("a", "b", "c"); + Assert.That (set.Count (), Is.EqualTo (3), "Count"); + Assert.That (set.Count (s => s.ToString () == "a"), Is.EqualTo (1), "Count predicate"); + } + + [Test] + public void NSOrderedSet_LongCount () + { + using var set = MakeOrderedSet ("a", "b"); + Assert.That (set.LongCount (), Is.EqualTo (2L), "LongCount"); + } + + [Test] + public void NSOrderedSet_Where () + { + using var set = MakeOrderedSet ("a", "bb", "ccc"); + var result = set.Where (s => s.Length > 1).ToList (); + Assert.That (result.Count, Is.EqualTo (2), "Where count"); + } + + [Test] + public void NSOrderedSet_Where_Index () + { + using var set = MakeOrderedSet ("a", "b", "c"); + var result = set.Where ((s, i) => i % 2 == 0).ToList (); + Assert.That (result.Count, Is.EqualTo (2), "Where index count"); + } + + [Test] + public void NSOrderedSet_Select () + { + using var set = MakeOrderedSet ("hello", "world"); + var lengths = set.Select (s => s.Length).ToList (); + Assert.That (lengths [0], Is.EqualTo (5), "Select [0]"); + } + + [Test] + public void NSOrderedSet_Select_Index () + { + using var set = MakeOrderedSet ("a", "b"); + var indexed = set.Select ((s, i) => $"{i}:{s}").ToList (); + Assert.That (indexed [0], Is.EqualTo ("0:a"), "Select index 0"); + } + + [Test] + public void NSOrderedSet_OrderBy () + { + using var set = MakeOrderedSet ("banana", "apple", "cherry"); + var sorted = set.OrderBy (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("apple"), "OrderBy first"); + } + + [Test] + public void NSOrderedSet_OrderByDescending () + { + using var set = MakeOrderedSet ("banana", "apple", "cherry"); + var sorted = set.OrderByDescending (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("cherry"), "OrderByDesc first"); + } + + [Test] + public void NSOrderedSet_Skip_Take () + { + using var set = MakeOrderedSet ("a", "b", "c", "d"); + var sliced = set.Skip (1).Take (2).ToList (); + Assert.That (sliced.Count, Is.EqualTo (2), "Skip/Take count"); + Assert.That (sliced [0].ToString (), Is.EqualTo ("b"), "Skip/Take [0]"); + } + + [Test] + public void NSOrderedSet_SkipWhile_TakeWhile () + { + using var set = MakeOrderedSet ("a", "b", "c"); + var skipped = set.SkipWhile (s => s.ToString () == "a").ToList (); + Assert.That (skipped.Count, Is.EqualTo (2), "SkipWhile count"); + var taken = set.TakeWhile (s => s.ToString () != "c").ToList (); + Assert.That (taken.Count, Is.EqualTo (2), "TakeWhile count"); + } + + [Test] + public void NSOrderedSet_Distinct () + { + using var set = MakeOrderedSet ("a", "b"); + var distinct = set.Distinct ().ToList (); + Assert.That (distinct.Count, Is.EqualTo (2), "Distinct count"); + } + + [Test] + public void NSOrderedSet_Reverse () + { + using var set = MakeOrderedSet ("a", "b", "c"); + var reversed = set.Reverse ().ToList (); + Assert.That (reversed [0].ToString (), Is.EqualTo ("c"), "Reverse [0]"); + Assert.That (reversed [2].ToString (), Is.EqualTo ("a"), "Reverse [2]"); + } + + [Test] + public void NSOrderedSet_Concat () + { + using var set = MakeOrderedSet ("a", "b"); + var all = set.Concat (new [] { S ("c") }).ToList (); + Assert.That (all.Count, Is.EqualTo (3), "Concat count"); + } + + [Test] + public void NSOrderedSet_ToList () + { + using var set = MakeOrderedSet ("a", "b"); + var list = set.ToList (); + Assert.That (list.Count, Is.EqualTo (2), "ToList count"); + } + + [Test] + public void NSOrderedSet_ToArray () + { + using var set = MakeOrderedSet ("a", "b"); + var arr = set.ToArray (); + Assert.That (arr.Length, Is.EqualTo (2), "ToArray length"); + } + + [Test] + public void NSOrderedSet_Aggregate () + { + using var set = MakeOrderedSet ("a", "b", "c"); + var result = set.Aggregate ((acc, s) => (NSString) (acc.ToString () + s.ToString ())); + Assert.That (result.ToString (), Is.EqualTo ("abc"), "Aggregate"); + } + + [Test] + public void NSOrderedSet_Aggregate_Seed () + { + using var set = MakeOrderedSet ("a", "b", "c"); + var result = set.Aggregate ("", (acc, s) => acc + s.ToString ()); + Assert.That (result, Is.EqualTo ("abc"), "Aggregate seed"); + } + + // ===== NSMutableOrderedSet ===== + + [Test] + public void NSMutableOrderedSet_First () + { + using var set = MakeMutableOrderedSet ("a", "b", "c"); + Assert.That (set.First ().ToString (), Is.EqualTo ("a"), "First"); + } + + [Test] + public void NSMutableOrderedSet_First_Empty_Throws () + { + using var set = MakeMutableOrderedSet (); + Assert.Throws (() => set.First ()); + } + + [Test] + public void NSMutableOrderedSet_FirstOrDefault_Empty () + { + using var set = MakeMutableOrderedSet (); + Assert.That (set.FirstOrDefault (), Is.Null, "FirstOrDefault empty"); + } + + [Test] + public void NSMutableOrderedSet_FirstOrDefault () + { + using var set = MakeMutableOrderedSet ("x", "y"); + Assert.That (set.FirstOrDefault ()?.ToString (), Is.EqualTo ("x"), "FirstOrDefault"); + } + + [Test] + public void NSMutableOrderedSet_FirstOrDefault_Predicate () + { + using var set = MakeMutableOrderedSet ("a", "b", "c"); + Assert.That (set.FirstOrDefault (s => s.ToString () == "b")?.ToString (), Is.EqualTo ("b"), "FirstOrDefault predicate match"); + Assert.That (set.FirstOrDefault (s => s.ToString () == "z"), Is.Null, "FirstOrDefault predicate no match"); + } + + [Test] + public void NSMutableOrderedSet_Last () + { + using var set = MakeMutableOrderedSet ("a", "b", "c"); + Assert.That (set.Last ().ToString (), Is.EqualTo ("c"), "Last"); + } + + [Test] + public void NSMutableOrderedSet_LastOrDefault_Empty () + { + using var set = MakeMutableOrderedSet (); + Assert.That (set.LastOrDefault (), Is.Null, "LastOrDefault empty"); + } + + [Test] + public void NSMutableOrderedSet_Single () + { + using var set = MakeMutableOrderedSet ("only"); + Assert.That (set.Single ().ToString (), Is.EqualTo ("only"), "Single"); + } + + [Test] + public void NSMutableOrderedSet_Single_Throws_OnMultiple () + { + using var set = MakeMutableOrderedSet ("a", "b"); + Assert.Throws (() => set.Single ()); + } + + [Test] + public void NSMutableOrderedSet_SingleOrDefault_Empty () + { + using var set = MakeMutableOrderedSet (); + Assert.That (set.SingleOrDefault (), Is.Null, "SingleOrDefault empty"); + } + + [Test] + public void NSMutableOrderedSet_ElementAt () + { + using var set = MakeMutableOrderedSet ("a", "b", "c"); + Assert.That (set.ElementAt (2).ToString (), Is.EqualTo ("c"), "ElementAt 2"); + } + + [Test] + public void NSMutableOrderedSet_ElementAtOrDefault () + { + using var set = MakeMutableOrderedSet ("a"); + Assert.That (set.ElementAtOrDefault (0)?.ToString (), Is.EqualTo ("a"), "in range"); + Assert.That (set.ElementAtOrDefault (5), Is.Null, "out of range"); + } + + [Test] + public void NSMutableOrderedSet_Any () + { + using var empty = MakeMutableOrderedSet (); + Assert.That (empty.Any (), Is.False, "Any empty"); + using var set = MakeMutableOrderedSet ("a"); + Assert.That (set.Any (), Is.True, "Any non-empty"); + } + + [Test] + public void NSMutableOrderedSet_All () + { + using var set = MakeMutableOrderedSet ("ab", "abc"); + Assert.That (set.All (s => s.Length > 1), Is.True, "All true"); + } + + [Test] + public void NSMutableOrderedSet_Count () + { + using var set = MakeMutableOrderedSet ("a", "b", "c"); + Assert.That (set.Count (), Is.EqualTo (3), "Count"); + Assert.That (set.Count (s => s.ToString () == "a"), Is.EqualTo (1), "Count predicate"); + } + + [Test] + public void NSMutableOrderedSet_LongCount () + { + using var set = MakeMutableOrderedSet ("a", "b"); + Assert.That (set.LongCount (), Is.EqualTo (2L), "LongCount"); + } + + [Test] + public void NSMutableOrderedSet_Where () + { + using var set = MakeMutableOrderedSet ("a", "bb", "ccc"); + var result = set.Where (s => s.Length > 1).ToList (); + Assert.That (result.Count, Is.EqualTo (2), "Where count"); + } + + [Test] + public void NSMutableOrderedSet_Select () + { + using var set = MakeMutableOrderedSet ("hello", "world"); + var lengths = set.Select (s => s.Length).ToList (); + Assert.That (lengths [0], Is.EqualTo (5), "Select [0]"); + } + + [Test] + public void NSMutableOrderedSet_OrderBy () + { + using var set = MakeMutableOrderedSet ("banana", "apple", "cherry"); + var sorted = set.OrderBy (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("apple"), "OrderBy first"); + } + + [Test] + public void NSMutableOrderedSet_OrderByDescending () + { + using var set = MakeMutableOrderedSet ("banana", "apple", "cherry"); + var sorted = set.OrderByDescending (s => s.ToString ()).ToList (); + Assert.That (sorted [0].ToString (), Is.EqualTo ("cherry"), "OrderByDesc first"); + } + + [Test] + public void NSMutableOrderedSet_Skip_Take () + { + using var set = MakeMutableOrderedSet ("a", "b", "c", "d"); + var sliced = set.Skip (1).Take (2).ToList (); + Assert.That (sliced.Count, Is.EqualTo (2), "Skip/Take count"); + Assert.That (sliced [0].ToString (), Is.EqualTo ("b"), "Skip/Take [0]"); + } + + [Test] + public void NSMutableOrderedSet_SkipWhile_TakeWhile () + { + using var set = MakeMutableOrderedSet ("a", "b", "c"); + var skipped = set.SkipWhile (s => s.ToString () == "a").ToList (); + Assert.That (skipped.Count, Is.EqualTo (2), "SkipWhile count"); + var taken = set.TakeWhile (s => s.ToString () != "c").ToList (); + Assert.That (taken.Count, Is.EqualTo (2), "TakeWhile count"); + } + + [Test] + public void NSMutableOrderedSet_Distinct () + { + using var set = MakeMutableOrderedSet ("a", "b"); + var distinct = set.Distinct ().ToList (); + Assert.That (distinct.Count, Is.EqualTo (2), "Distinct count"); + } + + [Test] + public void NSMutableOrderedSet_Reverse () + { + using var set = MakeMutableOrderedSet ("a", "b", "c"); + var reversed = set.Reverse ().ToList (); + Assert.That (reversed [0].ToString (), Is.EqualTo ("c"), "Reverse [0]"); + Assert.That (reversed [2].ToString (), Is.EqualTo ("a"), "Reverse [2]"); + } + + [Test] + public void NSMutableOrderedSet_Concat () + { + using var set = MakeMutableOrderedSet ("a", "b"); + var all = set.Concat (new [] { S ("c") }).ToList (); + Assert.That (all.Count, Is.EqualTo (3), "Concat count"); + } + + [Test] + public void NSMutableOrderedSet_ToList () + { + using var set = MakeMutableOrderedSet ("a", "b"); + var list = set.ToList (); + Assert.That (list.Count, Is.EqualTo (2), "ToList count"); + } + + [Test] + public void NSMutableOrderedSet_ToArray () + { + using var set = MakeMutableOrderedSet ("a", "b"); + var arr = set.ToArray (); + Assert.That (arr.Length, Is.EqualTo (2), "ToArray length"); + } + + [Test] + public void NSMutableOrderedSet_Aggregate () + { + using var set = MakeMutableOrderedSet ("a", "b", "c"); + var result = set.Aggregate ((acc, s) => (NSString) (acc.ToString () + s.ToString ())); + Assert.That (result.ToString (), Is.EqualTo ("abc"), "Aggregate"); + } + + [Test] + public void NSMutableOrderedSet_Aggregate_Seed () + { + using var set = MakeMutableOrderedSet ("a", "b", "c"); + var result = set.Aggregate ("", (acc, s) => acc + s.ToString ()); + Assert.That (result, Is.EqualTo ("abc"), "Aggregate seed"); + } + } +} diff --git a/tests/monotouch-test/Foundation/NSMutableArray1Test.cs b/tests/monotouch-test/Foundation/NSMutableArray1Test.cs index c5189f2b0944..63ada4fc6ce8 100644 --- a/tests/monotouch-test/Foundation/NSMutableArray1Test.cs +++ b/tests/monotouch-test/Foundation/NSMutableArray1Test.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Linq; namespace MonoTouchFixtures.Foundation { @@ -238,5 +239,13 @@ public void IEnumerable1Test_EnumeratorType () Console.WriteLine (item.LongValue); } } + + [Test] + public void LinqFirstOrDefaultTest () + { + using var arr = new NSMutableArray ((NSString) "1", (NSString) "2"); + var first = arr.FirstOrDefault (); + Assert.That (first, Is.EqualTo ((NSString) "1"), "FirstOrDefault"); + } } } diff --git a/tests/monotouch-test/Foundation/NSSet1Test.cs b/tests/monotouch-test/Foundation/NSSet1Test.cs index 782309ea9224..6e049a3f35d3 100644 --- a/tests/monotouch-test/Foundation/NSSet1Test.cs +++ b/tests/monotouch-test/Foundation/NSSet1Test.cs @@ -294,6 +294,15 @@ public void IEnumerable1Test_EnumeratorType () } } + [Test] + public void LinqFirstOrDefaultTest () + { + using var st = new NSSet ((NSString) "1", (NSString) "2"); + var first = st.FirstOrDefault (); + Assert.That (first, Is.Not.Null, "FirstOrDefault"); + Assert.That (st.Contains (first), Is.True, "Contains"); + } + [Test] public void IEnumerableTest () {