Sweetener 1.0.0-beta.3

.NET 6.0 .NET Standard 2.0
This is a prerelease version of Sweetener.

Requires NuGet 4.9.0 or higher.

dotnet add package Sweetener --version 1.0.0-beta.3
NuGet\Install-Package Sweetener -Version 1.0.0-beta.3
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="Sweetener" Version="1.0.0-beta.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sweetener --version 1.0.0-beta.3
#r "nuget: Sweetener, 1.0.0-beta.3"
#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 Sweetener as a Cake Addin
#addin nuget:?package=Sweetener&version=1.0.0-beta.3&prerelease

// Install Sweetener as a Cake Tool
#tool nuget:?package=Sweetener&version=1.0.0-beta.3&prerelease

Sweetener

GitHub License Sweetener Build Sweetener Code Coverage

The Sweetener library contains a set of APIs that extends the .NET Base Class Libraries (BCLs). New functionality is built on top of both the BCLs' data types and the new types introduced by Sweetener. This README provides examples for a subset of these new APIs.

New Types

Sweetener introduces new types for common scenarios.

Delegates

In addition to Action and Func<TOut>, Sweetener adds asynchronous and "try" delegates.

using System.IO;
using Sweetener;

// TryFunc<TOut>
TryFunc<string, int> tryParse = int.TryParse;
bool result = tryParse("12345", out int number);

// AsyncAction
using StreamWriter writer = /* ... */;
AsyncAction<string?> writeLineAsync = writer.WriteLineAsync;
await writeLineAsync("Hello World!").ConfigureAwait(false);

// AsyncFunc<TOut>
using StreamReader reader = /* ... */;
AsyncFunc<string?> readLineAsync = reader.ReadLineAsync;
string? line = await readLineAsync().ConfigureAwait(false);

Optional<T>

While Nullable<T> creates a type union of null and value types, the Optional<T> class represents some value of type T or no value at all. In this way, methods can choose to optionally return a value of type T. This may be especially helpful for asynchronous methods that attempt to return a value but are unable to use out parameters.

using System;
using Sweetener;

Optional<string?> response = await TryReadMessageAsync().ConfigureAwait(false);
if (response.TryGetValue(out string? message))
    Console.WriteLine("Received: " + message);
else
    Console.WriteLine("Did not receive response");

New Functionality

Sweetener adds additional functionality for many of the BCL APIs.

BinaryComparer

For data types that represent binary data, like byte[], Stream, and ReadOnlySpan<byte>, comparisons can be made using the BinaryComparer class.

using System.Collections.Generic;
using Sweetener.Collections.Generic;

byte[] a = /* ... */;
byte[] b = /* ... */;

// IComparer<T>
IComparer<byte[]> comparer = BinaryComparer.Instance;
int cmp = comparer.Compare(a, b);

// IEqualityComparer<T>
IEqualityComparer<byte[]> equalityComparer = BinaryComparer.Instance;
bool areEqual = equalityComparer.Equals(a, b);
int hashCode = equalityComparer.GetHashCode(a);

MultiTask

Unlike Task.WhenAll(Task[]), the MultiTask class allows users to wait on the completion of multiple heterogeneous tasks whose type arguments differ from one another.

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

Task<int> task1 = /* ... */;
Task<string> task2 = /* ... */;
Task<DateTime> task3 = /* ... */;

(int n, string s, DateTime d) = await MultiTask.WhenAll(task1, task2, task3).ConfigureAwait(false);
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Sweetener:

Package Downloads
Sweetener.Reliability

API for executing user-defined delegates despite transient failures

Sweetener.Linq

APIs that sweeten System.Linq in the .NET base class libraries

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-beta.3 324 6/11/2022
1.0.0-beta.2 138 5/11/2021
1.0.0-beta.1 363 6/2/2020
1.0.0-alpha.2 280 3/20/2020