https://github.com/ricardotondello/functionalenumerableextensions
These extensions are designed to enhance memory efficiency and make common operations more convenient. Whether you're converting to lists, arrays, or working with spans, these extensions aim to improve your code's performance and readability.
https://github.com/ricardotondello/functionalenumerableextensions
csharp dotnet dotnetcore enumerable extensions
Last synced: over 1 year ago
JSON representation
These extensions are designed to enhance memory efficiency and make common operations more convenient. Whether you're converting to lists, arrays, or working with spans, these extensions aim to improve your code's performance and readability.
- Host: GitHub
- URL: https://github.com/ricardotondello/functionalenumerableextensions
- Owner: ricardotondello
- License: mit
- Created: 2023-08-11T11:52:52.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-25T20:36:55.000Z (over 1 year ago)
- Last Synced: 2025-02-25T21:32:40.707Z (over 1 year ago)
- Topics: csharp, dotnet, dotnetcore, enumerable, extensions
- Language: C#
- Homepage:
- Size: 53.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# FunctionalEnumerableExtensions 🌟
[](https://github.com/ricardotondello/FunctionalEnumerableExtensions/actions/workflows/dotnet.yml)
[](https://sonarcloud.io/dashboard?id=ricardotondello_FunctionalEnumerableExtensions)
[](https://sonarcloud.io/component_measures?id=ricardotondello_FunctionalEnumerableExtensions&metric=coverage)
[](https://nuget.org/packages/FunctionalEnumerableExtensions)
[](https://www.nuget.org/packages/FunctionalEnumerableExtensions)
`FunctionalEnumerableExtensions` is a C# class library that provides a set of extension methods for working with enumerable collections.
These extensions are designed to enhance memory efficiency and make common operations more convenient.
Whether you're converting to lists, arrays, or working with spans, these extensions aim to improve your code's performance and readability.
## Installation 🚀
To easily integrate the FunctionalEnumerableExtensions library into your project, you can use NuGet Package Manager.
NuGet is a package manager for .NET that simplifies the process of adding, removing,
and updating libraries in your applications.
After that import the `FunctionalEnumerableExtensions` namespace in your code files where you want to use the provided extension methods:
```csharp
using FunctionalEnumerableExtensions;
```
## Available Extension Methods 🛠️
### `EnsureList`
Prevent memory allocation when converting to a list using LINQ's `.ToList()`.
**Usage:**
```csharp
List myList = myEnumerable.EnsureList();
```
### `EnsureArray`
Prevent memory allocation when converting to an array using LINQ's `.ToArray()`.
**Usage:**
```csharp
T[] myArray = myEnumerable.EnsureArray();
```
### `AsSpan`
**Warning: DO NOT use Span if you would change the list while looping into it, it can cause exceptions.**
Convert the enumerable collection to a Span, suitable for in-place data processing.
**Usage:**
```csharp
Span mySpan = myEnumerable.AsSpan();
```
### `EnsureHashSet`
Prevent memory allocation by casting an IEnumerable to a HashSet if it's already of that type, otherwise create a new HashSet.
**Usage:**
```csharp
HashSet myHashSet = myEnumerable.EnsureHashSet();
```
### `CollectNonNulls`
Filter out non-null items from the input IEnumerable.
**Usage:**
```csharp
var myFilteredList = myEnumerable.CollectNonNulls();
```
### `EnsureEnumerable`
Prevent the enumerable to be null.
**Usage:**
```csharp
var myNotNullEnumerable = myEnumerable.EnsureEnumerable();
```
### `SplitBy`
Splits the list according to the predicate.
**Usage:**
```csharp
var (desiredItems, remainingItems) = enumerable.SplitBy(customer => customer.LoyaltyTimeInYears > 20);
```
### `IsNullOrEmpty`
Checks if the list is null or empty
**Usage:**
```csharp
var result = enumerable.IsNullOrEmpty();
```
### `WhereIf`
Introduces optional filtering, applying a predicate only if a specified condition holds true.
**Usage:**
```csharp
var result = enumerable.WhereIf(YourBooleanCondition(), w => w > 0);
```
### `Stringify`
Converts a collection of non-null objects to a string by concatenating their properties recursively, separated by commas.
**Usage:**
```csharp
var result = enumerable.Stringify();
```
### `EnumerateWithIndex`
Enumerate an IEnumerable source and getting the Index and the Item returned in a ValueTuple.
**Usage**
```csharp
var result = enumerable.EnumerateWithIndex();
```
### `JoinString`
Chainable extensions that joins the separator string with the elements of your Enumerable.
**Usage**
```csharp
var result = list.Select(s => s.Name).JoinString();
```
### `OrderBy` and `OrderByDescending` with a delegate comparer
Sorts the elements of a sequence in ascending/descending order by using a specified comparer.
**Usage**
```csharp
record Customer(string Name, int Age);
//Order by name then by age
//user OrderByDescending to order descendingly
var result = values.OrderBy(x => (x.Name, x.Age), (a, b) =>
{
var nameComparison = string.Compare(a.Name, b.Name, StringComparison.Ordinal);
return nameComparison != 0 ? nameComparison : a.Age.CompareTo(b.Age);
});
```
## Contributing 👥
Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub.
If you would like to contribute code, please fork the repository and submit a pull request.
## License 📄
This project is licensed under the MIT License.
See [LICENSE](https://github.com/ricardotondello/FunctionalEnumerableExtensions/blob/main/LICENSE) for more information.
## Support ☕
