CodeOfChaos.Extensions 0.26.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CodeOfChaos.Extensions --version 0.26.0                
NuGet\Install-Package CodeOfChaos.Extensions -Version 0.26.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeOfChaos.Extensions" Version="0.26.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeOfChaos.Extensions --version 0.26.0                
#r "nuget: CodeOfChaos.Extensions, 0.26.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CodeOfChaos.Extensions as a Cake Addin
#addin nuget:?package=CodeOfChaos.Extensions&version=0.26.0

// Install CodeOfChaos.Extensions as a Cake Tool
#tool nuget:?package=CodeOfChaos.Extensions&version=0.26.0                

CodeOfChaos.Extensions

CodeOfChaos.Extensions is a collection of utility extension methods designed to enhance and simplify everyday programming tasks in .NET projects. This library offers extensions for tasks, strings, enums, LINQ queries, collections, and dictionaries, enabling more readable, efficient, and intuitive code.


Features

Task Extensions

Enhance task handling with support for timeouts and cancellation tokens:

  • .WithCancellation(CancellationToken) – Cancel a task when the token is triggered.
  • .WithTimeout(TimeSpan) – Enforce a timeout on task execution.
  • Available for both Task and Task<T>.

String Extensions

Simplify string management with useful utilities:

  • IsNullOrEmpty() / IsNotNullOrEmpty() – Check for null or empty strings.
  • IsNullOrWhiteSpace() / IsNotNullOrWhiteSpace() – Check for null, empty, or whitespace strings.
  • Truncate(int maxLength) – Shorten a string to a maximum length.
  • ToGuid() – Parse a string into a Guid.

Enum Extensions

Work with enums more efficiently:

  • .GetFlags() – Retrieve all flagged values from an enum.
  • .GetFlagsAsArray() / .GetFlagsAsList() – Retrieve flagged values as arrays or lists.
  • Caches enums for better performance.

LINQ Extensions

Add conditional logic to LINQ operations:

  • Apply operations conditionally:
    • ConditionalWhere
    • ConditionalTake
    • ConditionalOrderBy
    • ConditionalDistinct and more.
  • Chain LINQ operations dynamically based on runtime conditions.

Collection Extensions

Extensions to handle collections and arrays more effectively:

  • IsEmpty() – Check if a string[] or IEnumerable<string> is empty.

Dictionary Extensions

Enhance dictionary management:

  • AddOrUpdate() – Add a key-value pair or update the existing value.
  • TryAddToOrCreateCollection() – Add values to a collection within a dictionary or create a new collection.

Installation

This library targets .NET 9.0 and requires C# 13.0. Ensure your project meets these requirements before using.

Add the dependency to your project via NuGet:

dotnet add package CodeOfChaos.Extensions

Usage

Here’s how you can leverage the CodeOfChaos.Extensions library:

Task Example

using System;
using System.Threading;
using System.Threading.Tasks;

var tokenSource = new CancellationTokenSource();
var task = Task.Delay(5000); // Example task
await task.WithCancellation(tokenSource.Token);

String Example

using System;

string? input = "Hello, World!";
if (input.IsNotNullOrWhiteSpace()) {
    Console.WriteLine(input.Truncate(5)); // Output: Hello
}

Enum Example

[Flags]
enum AccessLevel { None = 0, Read = 1, Write = 2, Execute = 4 }

var access = AccessLevel.Read | AccessLevel.Execute;
var flags = access.GetFlagsAsList(); // Output: [Read, Execute]

LINQ Example

var items = new List<int> { 1, 2, 3, 4, 5 }.AsQueryable();
var filtered = items.ConditionalWhere(true, x => x > 2); // Applies condition

Contributing

Feel free to fork and contribute to the project by submitting pull requests. When contributing, ensure your changes align with the project’s coding standards.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on CodeOfChaos.Extensions:

Package Downloads
CodeOfChaos.AspNetCore

A Library of Nuget Packages frequently used in other code-of-chaos projects or project written by Andreas

CodeOfChaos.Parsers.Csv

A Library to parse csv files in a similar api to regular xml parsing

CodeOfChaos.Types

A library of common types and classes within the CodeOfChaos project

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.27.0 45 12/29/2024
0.26.0 51 12/28/2024
0.25.0 52 12/28/2024
0.24.3 44 12/27/2024
0.24.2 38 12/27/2024
0.24.1 39 12/27/2024
0.24.0 41 12/27/2024
0.23.1 40 12/27/2024
0.23.0 45 12/27/2024
0.22.2 43 12/27/2024
0.22.0 95 12/23/2024
0.21.0 84 12/22/2024
0.20.0-preview.1 52 12/21/2024
0.15.0 79 12/11/2024
0.13.0 125 11/1/2024
0.12.1 93 10/31/2024
0.12.0 88 10/30/2024
0.11.0 100 10/30/2024
0.10.0 86 10/26/2024
0.9.0 89 10/25/2024
0.8.1 135 9/18/2024
0.7.0 133 9/4/2024
0.6.0 192 7/6/2024
0.5.0 93 7/6/2024
0.4.3 108 7/1/2024
0.4.1 108 7/1/2024
0.4.0 216 6/13/2024
0.3.0 99 6/11/2024
0.2.0 149 6/8/2024
0.1.0 104 6/7/2024