Skip to content

IEnumerable Extensions

Andrzej Kebab edited this page Jan 24, 2024 · 6 revisions

UtilityLibrary.Core

IsNullOrEmpty

Checks if the IEnumerable is null or empty.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
using System.Linq;

List<int> list1 = new List<int>();
List<int> list2 = null;

bool isNullOrEmpty1 = list1.IsNullOrEmpty();
bool isNullOrEmpty2 = list2.IsNullOrEmpty();

// Result: isNullOrEmpty1 = true, isNullOrEmpty2 = true

IsEmpty

Checks if the IEnumerable is empty, throwing an exception if it is null.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;

List<int> list = new List<int>();

bool isEmpty = list.IsEmpty();

// Result: isEmpty = true

IsNotEmpty

Checks if the IEnumerable is not empty.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;

List<int> list = new List<int> { 1, 2, 3 };

bool isNotEmpty = list.IsNotEmpty();

// Result: isNotEmpty = true

Has

Determines whether the collection contains elements that match the conditions defined by the specified predicate.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;

List<int> list = new List<int> { 1, 2, 3, 4, 5 };

int result = list.Has(x => x > 3);

// Result: result = 4

Find (First Occurrence)

Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire IEnumerable.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;

List<int> list = new List<int> { 1, 2, 3, 4, 5 };

int result = list.Find(x => x > 3);

// Result: result = 4

FindAll

Retrieves all the elements that match the conditions defined by the specified predicate.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;

List<int> list = new List<int> { 1, 2, 3, 4, 5 };

List<int> result = list.FindAll(x => x > 2);

// Result: result = [3, 4, 5]

Except

Produces the set difference of two sequences by using the default equality comparer to compare values.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
using System.Linq;

List<int> source = new List<int> { 1, 2, 3, 4, 5 };
List<int> args = new List<int> { 3, 4, 5, 6, 7 };

IEnumerable<int> result = source.Except(args);

// Result: result = [1, 2]

Without

Filters a sequence of values based on a predicate. In this case, the predicate is a function that identifies the item that should be excluded.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
using System.Linq;

List<int> source = new List<int> { 1, 2, 3, 4, 5 };
int item = 3;

IEnumerable<int> result = source.Without(item);

// Result: result = [1, 2, 4, 5]

Convert to Dictionary

Converts a sequence of value tuples to a Dictionary.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;

IEnumerable<(string, int)> values = new List<(string, int)> { ("One", 1), ("Two", 2) };

Dictionary<string, int> result = values.ToDictionary();

// Result: result = { "One": 1, "Two": 2 }

IsEquivalentTo

Determines whether two sequences are equivalent by comparing their elements using the default equality comparer.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;

List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int> { 3, 2, 1 };

bool isEquivalent = list1.IsEquivalentTo(list2);

// Result: isEquivalent = true

ToHashSet

Converts a sequence to a HashSet.

using UtilityLibrary.Core;
using System;
using System.Collections.Generic;

List<int> list = new List<int> { 1, 2, 3, 4, 5 };

HashSet<int> result = list.ToHashSet();

// Result: result = { 1, 2, 3, 4, 5 }

UtilityLibrary.Core.LinqReplacement

Any

Determines whether a sequence contains any elements.

using UtilityLibrary.Core.LinqReplacement;

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

bool hasAnyNumbers = numbers.Any();

// Result: true

Any with Predicate

Determines whether any element of a sequence satisfies a condition.

using UtilityLibrary.Core.LinqReplacement;

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

bool hasEvenNumber = numbers.Any(x => x % 2 == 0);

// Result: true

First

Returns the first element of a sequence, or a default value if the sequence contains no elements.

using UtilityLibrary.Core.LinqReplacement;

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int firstNumber = numbers.First();

// Result: 1

First (with Count)

Returns the first specified number of elements from a sequence.

using UtilityLibrary.Core.LinqReplacement;

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

List<int> firstTwoNumbers = numbers.First(2);

// Result: [1, 2]

Last

Returns the last element of a sequence, or a default value if the sequence contains no elements.

using UtilityLibrary.Core.LinqReplacement;

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int lastNumber = numbers.Last();

// Result: 5

Last (with Count)

Returns the last specified number of elements from a sequence.

using UtilityLibrary.Core.LinqReplacement;

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

List<int> lastTwoNumbers = numbers.Last(2);

// Result: [4, 5]

ToArray

Creates an array from an IEnumerable<T>.

using UtilityLibrary.Core.LinqReplacement;

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int[] numbersArray = numbers.ToArray();

// Result: array[1, 2, 3, 4, 5]

ToList

Creates a List<T> from an IEnumerable<T>.

using UtilityLibrary.Core.LinqReplacement;

IEnumerable<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

List<int> numbersList = numbers.ToList();

// Result: list[1, 2, 3, 4, 5]

Min / Max

Min

Returns the minimum item in the source sequence based on a key selector function.

// Example usage
Enumerable<int> intSequence = /* Instantiate or reference a sequence of integers */;
int minValue = intSequence.Min(i => i);
Debug.Log($"Minimum value in the sequence: {minValue}");

Parameters:

  • source (IEnumerable<T>): The sequence to find the minimum item in.
  • keyGetter (Func<T, TValue>): A function to extract the comparison key from each element.
  • minValue (out TValue): The minimum value found in the sequence.

Returns:

  • T: The item with the minimum value in the source sequence.

Max

Returns the maximum item in the source sequence based on a key selector function.

// Example usage
Enumerable<int> intSequence = /* Instantiate or reference a sequence of integers */;
int maxValue = intSequence.Max(i => i);
Debug.Log($"Maximum value in the sequence: {maxValue}");

Parameters:

  • source (IEnumerable<T>): The sequence to find the maximum item in.
  • keyGetter (Func<T, TValue>): A function to extract the comparison key from each element.
  • maxValue (out TValue): The maximum value found in the sequence.

Returns:

  • T: The item with the maximum value in the source sequence.

Custom Comparisons

Both Min and Max methods also support custom comparisons by providing a key selector function.

// Example usage with custom comparison
Enumerable<string> stringSequence = /* Instantiate or reference a sequence of strings */;
string minLengthString = stringSequence.Min(str => str.Length, out int minLength);
Debug.Log($"String with minimum length ({minLength}): {minLengthString}");

Parameters:

  • source (IEnumerable<T>): The sequence to find the minimum or maximum item in.
  • keyGetter (Func<T, TValue>): A function to extract the comparison key from each element.
  • minValue (out TValue): The minimum value found in the sequence.

Returns:

  • T: The item with the minimum or maximum value in the source sequence based on the provided key.

Select

Select

Projects each element of a sequence into a new form.

// Example usage
Enumerable<double> doubleSequence = /* Instantiate or reference a sequence of doubles */;
Enumerable<int> intResult = doubleSequence.Select(d => (int)d);
Debug.Log($"Projected sequence of doubles to integers: {string.Join(", ", intResult)}");

Parameters:

  • source (IEnumerable<T>): The sequence to project.
  • selector (Func<T, TResult>): A transform function to apply to each element.
  • allowNull (bool): A boolean value to determine whether null values are allowed. Default is true.

Returns:

  • IEnumerable<TResult>: A sequence whose elements are the result of invoking the transform function on each element of the source sequence.

Clone this wiki locally